home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Graphics / Plotting / aa_Intel_Only / Gnuplot / GnuplotSource / command.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-06-12  |  111.8 KB  |  4,272 lines

  1. #ifndef lint
  2. static char    *RCSid = "$Id: command.c%v 3.38.2.119 1993/04/26 00:02:13 woo Exp woo $";
  3. #endif
  4.  
  5.  
  6. /* GNUPLOT - command.c */
  7. /*
  8.  * Copyright (C) 1986 - 1993   Thomas Williams, Colin Kelley
  9.  * 
  10.  * Permission to use, copy, and distribute this software and its documentation
  11.  * for any purpose with or without fee is hereby granted, provided that the
  12.  * above copyright notice appear in all copies and that both that copyright
  13.  * notice and this permission notice appear in supporting documentation.
  14.  * 
  15.  * Permission to modify the software is granted, but not the right to distribute
  16.  * the modified code.  Modifications are to be distributed as patches to
  17.  * released version.
  18.  * 
  19.  * This software is provided "as is" without express or implied warranty.
  20.  * 
  21.  * 
  22.  * AUTHORS
  23.  * 
  24.  * Original Software: Thomas Williams,  Colin Kelley.
  25.  * 
  26.  * Gnuplot 2.0 additions: Russell Lang, Dave Kotz, John Campbell.
  27.  * 
  28.  * Gnuplot 3.0 additions: Gershon Elber and many others.
  29.  * 
  30.  * Changes:
  31.  * 
  32.  * Feb 5, 1992    Jack Veenstra    (veenstra@cs.rochester.edu) Added support to
  33.  * filter data values read from a file through a user-defined function before
  34.  * plotting. The keyword "thru" was added to the "plot" command. Example
  35.  * syntax: f(x) = x / 100 plot "test.data" thru f(x) This example divides all
  36.  * the y values by 100 before plotting. The filter function processes the
  37.  * data before any log-scaling occurs. This capability should be generalized
  38.  * to filter x values as well and a similar feature should be added to the
  39.  * "splot" command.
  40.  * 
  41.  * 19 September 1992  Lawrence Crowl  (crowl@cs.orst.edu)
  42.  * Added user-specified bases for log scaling.
  43.  * 
  44.  * Send your comments or suggestions to info-gnuplot@dartmouth.edu. This is
  45.  * a mailing list; to join it send a note to
  46.  * info-gnuplot-request@dartmouth.edu.  Send bug reports to
  47.  * bug-gnuplot@dartmouth.edu.
  48.  */
  49.  
  50. #include <stdio.h>
  51. #include <math.h>
  52. #include <ctype.h>
  53.  
  54. #ifdef AMIGA_AC_5
  55. #include <time.h>
  56. void            sleep();    /* defined later */
  57. #endif
  58.  
  59. #ifdef OS2
  60. #include <setjmp.h>
  61. extern jmp_buf env;       /* from plot.c */
  62. #endif
  63.  
  64. #if defined(MSDOS) || defined(DOS386)
  65. #ifdef DJGPP
  66. #include <dos.h>
  67. #else
  68. #include <process.h>
  69. #endif
  70.  
  71. #ifdef __ZTC__
  72. #define P_WAIT 0
  73. #include <time.h>        /* usleep() */
  74. #else
  75.  
  76. #ifdef __TURBOC__
  77. #ifndef _Windows
  78. #include <dos.h>        /* sleep() */
  79. #include <conio.h>
  80. #include <dir.h>    /* setdisk() */
  81. extern unsigned _stklen = 16394;/* increase stack size */
  82. #endif
  83.  
  84. #else                /* must be MSC */
  85. #if !defined(__EMX__) && !defined(DJGPP)
  86. #ifdef __MSC__
  87. #include <direct.h>        /* for _chdrive() */
  88. #endif
  89. #include <time.h>        /* kludge to provide sleep() */
  90. void            sleep();    /* defined later */
  91. #endif                /* !__EMX__ && !DJGPP */
  92. #endif                /* TURBOC */
  93. #endif                /* ZTC */
  94.  
  95. #endif                /* MSDOS */
  96.  
  97. #ifdef ATARI
  98. #ifdef __PUREC__
  99. #include <ext.h>
  100. #include <tos.h>
  101. #include <aes.h>
  102. #else
  103. #include <osbind.h>
  104. #include <aesbind.h>
  105. #endif /* __PUREC__ */
  106. #endif /* ATARI */
  107.  
  108. #ifdef AMIGA_SC_6_1
  109. #include <proto/dos.h>
  110. void            sleep();
  111. #endif                /* AMIGA_SC_6_1 */
  112.  
  113. #include "plot.h"
  114. #include "setshow.h"
  115. #ifndef _Windows
  116. #include "help.h"
  117. #else
  118. #define MAXSTR 255
  119. #endif
  120.  
  121. #ifndef STDOUT
  122. #define STDOUT 1
  123. #endif
  124.  
  125. #ifndef HELPFILE
  126. #if defined( MSDOS ) || defined( OS2 ) || defined(DOS386)
  127. #define HELPFILE "gnuplot.gih"
  128. #else
  129. #ifdef AMIGA_SC_6_1
  130. #define HELPFILE "S:gnuplot.gih"
  131. #else
  132. #define HELPFILE "docs/gnuplot.gih"    /* changed by makefile */
  133. #endif                /* AMIGA_SC_6_1 */
  134. #endif
  135. #endif                /* HELPFILE */
  136.  
  137. #ifdef _Windows
  138. #include <windows.h>
  139. #include <setjmp.h>
  140. #ifdef __MSC__
  141. #include <malloc.h>
  142. #else
  143. #include <alloc.h>
  144. #include <dir.h>    /* setdisk() */
  145. #endif
  146. #include "win/wgnuplib.h"
  147. void sleep();
  148. extern TW textwin;
  149. extern jmp_buf FAR env;       /* from plot.c */
  150. extern LPSTR winhelpname;
  151. extern void screen_dump(void);    /* in term/win.trm */
  152. extern int Pause(LPSTR mess); /* in winmain.c */
  153. #endif
  154.  
  155. #define inrange(z,min,max) ((min<max) ? ((z>=min)&&(z<=max)) : ((z>=max)&&(z<=min)) )
  156.  
  157. /*
  158.  * instead of <strings.h>
  159.  */
  160.  
  161. #ifndef ATARI
  162. #ifdef _Windows
  163. #include <string.h>
  164. #include <stdlib.h>
  165. #else
  166. #ifndef AMIGA_SC_6_1
  167. extern char    *gets(), *getenv();
  168. #ifdef sequent
  169. extern char    *strcpy(), *strncpy(), *strcat(), *index();
  170. #else
  171. extern char    *strcpy(), *strncpy(), *strcat(), *strchr();
  172. #endif
  173. extern int      strlen(), strcmp();
  174. extern double   atof();
  175. #endif /* !AMIGA_SC_6_1 */
  176. #endif
  177. #else
  178. #ifdef __PUREC__
  179. /*
  180.  * a substitute for PureC's buggy sscanf.
  181.  * this uses the normal sscanf and fixes the following bugs:
  182.  * - whitespace in format matches whitespace in string, but doesn't
  183.  *   require any. ( "%f , %f" scans "1,2" correctly )
  184.  * - the ignore value feature works (*). this created an address error
  185.  *   in PureC.
  186.  */
  187.  
  188. #include <stdarg.h>
  189. #include <string.h>
  190.  
  191. int purec_sscanf( const char *string, const char *format, ... )
  192. {
  193.   va_list args;
  194.   int cnt=0;
  195.   char onefmt[256];
  196.   char buffer[256];
  197.   const char *f=format;
  198.   const char *s=string;
  199.   char *f2;
  200.   char ch;
  201.   int ignore;
  202.   void *p;
  203.   int *ip;
  204.   int pos;
  205.  
  206.   va_start(args,format);
  207.   while( *f && *s ) {
  208.     ch=*f++;
  209.     if( ch!='%' ) {
  210.       if(isspace(ch)) {
  211.         /* match any number of whitespace */
  212.         while(isspace(*s)) s++;
  213.       } else {
  214.         /* match exactly the character ch */
  215.         if( *s!=ch ) goto finish;
  216.         s++;
  217.       }
  218.     } else {
  219.       /* we have got a '%' */
  220.       ch=*f++;
  221.       if( ch=='%' ) {
  222.         /* match exactly % */
  223.         if( *s!=ch ) goto finish;
  224.         s++;
  225.       } else {
  226.         f2=onefmt;
  227.         *f2++='%';
  228.         *f2++=ch;
  229.         ignore=0;
  230.         if( ch=='*' ) {
  231.           ignore=1;
  232.           ch=f2[-1]=*f++;
  233.         }
  234.         while( isdigit(ch) ) {
  235.           ch=*f2++=*f++;
  236.         }
  237.         if( ch=='l' || ch=='L' || ch=='h' ) {
  238.           ch=*f2++=*f++;
  239.         }
  240.         switch(ch) {
  241.           case '[':
  242.             while( ch && ch!=']' ) {
  243.               ch=*f2++=*f++;
  244.             }
  245.             if( !ch ) goto error;
  246.             break;
  247.           case 'e':
  248.           case 'f':
  249.           case 'g':
  250.           case 'd':
  251.           case 'o':
  252.           case 'i':
  253.           case 'u':
  254.           case 'x':
  255.           case 'c':
  256.           case 's':
  257.           case 'p':
  258.           case 'n': /* special case handled below */
  259.             break;
  260.           default:
  261.             goto error;
  262.         }
  263.         if( ch!='n' ) {
  264.           strcpy(f2,"%n");
  265.           if( ignore ) {
  266.             p=buffer;
  267.           } else {
  268.             p=va_arg(args,void *);
  269.           }
  270.           switch( sscanf( s, onefmt, p, &pos ) ) {
  271.             case EOF: goto error;
  272.             case  0 : goto finish;
  273.           }
  274.           if( !ignore ) cnt++;
  275.           s+=pos;
  276.         } else {
  277.           if( !ignore ) {
  278.             ip=va_arg(args,int *);
  279.             *ip=(int)(s-string);
  280.           }
  281.         }
  282.       }
  283.     }
  284.   }
  285.  
  286.   if( !*f ) goto finish;
  287.  
  288. error:
  289.   cnt=EOF;
  290. finish:
  291.   va_end(args);
  292.   return cnt;
  293. }
  294.  
  295. /* use the substitute now. I know this is dirty trick, but it works. */
  296. #define sscanf purec_sscanf
  297.  
  298. #endif /* __PUREC__ */
  299. #endif /* ATARI */
  300.  
  301. /*
  302.  * Only reference to contours library.
  303.  */
  304. extern struct gnuplot_contours *contour();
  305.  
  306. #ifdef OS2
  307.  /* emx has getcwd, chdir that can handle drive names */
  308. #define getcwd _getcwd2
  309. #define chdir  _chdir2
  310. #endif /* OS2 */
  311.  
  312. #if defined(unix) && !defined(hpux)
  313. #ifdef GETCWD
  314. extern char    *getcwd();    /* some Unix's use getcwd */
  315. #else
  316. extern char    *getwd();    /* most Unix's use getwd */
  317. #endif
  318. #else
  319. #ifdef DJGPP
  320. extern char    *getwd();    /* DJGPP acts like Unix here */
  321. #else
  322. extern char    *getcwd();    /* Turbo C, MSC, EMX, OS2 and VMS use getcwd */
  323. #endif
  324. #endif
  325.  
  326. #ifdef vms
  327. int             vms_vkid;    /* Virtual keyboard id */
  328. #endif
  329.     
  330. static FILE *data_fp=NULL;    /* != means file still open */
  331. static TBOOLEAN more_data_fp=FALSE;  /* And this explicitly says so. */
  332.  
  333. #if defined(unix) || defined(PIPES)
  334. extern FILE    *popen();
  335. static TBOOLEAN  pipe_open = FALSE;
  336. #endif
  337.  
  338. extern int      chdir();
  339.  
  340. extern double   magnitude(), angle(), real(), imag();
  341. extern struct value *const_express(), *pop(), *Gcomplex();
  342. extern struct at_type *temp_at(), *perm_at();
  343. extern struct udft_entry *add_udf();
  344. extern struct udvt_entry *add_udv();
  345. extern void     squash_spaces();
  346. extern void     lower_case();
  347.  
  348. /* local functions */
  349. static enum coord_type adjustlog();
  350.  
  351. extern TBOOLEAN  interactive;    /* from plot.c */
  352.  
  353. /* input data, parsing variables */
  354. struct lexical_unit token[MAX_TOKENS];
  355. char            input_line[MAX_LINE_LEN + 1] = "";
  356. int             num_tokens, c_token;
  357. int             inline_num = 0;    /* input line number */
  358.  
  359. char            c_dummy_var[MAX_NUM_VAR][MAX_ID_LEN + 1];    /* current dummy vars */
  360.  
  361. /* the curves/surfaces of the plot */
  362. struct curve_points *first_plot = NULL;
  363. struct surface_points *first_3dplot = NULL;
  364. static struct udft_entry plot_func;
  365. struct udft_entry *dummy_func;
  366.  
  367. /* jev -- for passing data thru user-defined function */
  368. static struct udft_entry ydata_func;
  369.  
  370. /* support for replot command */
  371. char            replot_line[MAX_LINE_LEN + 1] = "";
  372. static int      plot_token;    /* start of 'plot' command */
  373.  
  374. /* If last plot was a 3d one. */
  375. TBOOLEAN         is_3d_plot = FALSE;
  376.  
  377. com_line()
  378. {
  379.    if (read_line(PROMPT))
  380.        return(1);
  381.  
  382.     /* So we can flag any new output: if false at time of error, */
  383.     /* we reprint the command line before printing caret. */
  384.     /* TRUE for interactive terminals, since the command line is typed. */
  385.     /* FALSE for non-terminal stdin, so command line is printed anyway. */
  386.     /* (DFK 11/89) */
  387.     screen_ok = interactive;
  388.  
  389.     if (do_line())
  390.         return(1);
  391.      else
  392.         return(0);
  393. }
  394.  
  395.  
  396. do_line()
  397. {                /* also used in load_file */
  398.     if (is_system(input_line[0])) {
  399.     do_system();
  400.     (void) fputs("!\n", stderr);
  401.     return(0);
  402.     }
  403.     num_tokens = scanner(input_line);
  404.     c_token = 0;
  405.     while (c_token < num_tokens) {
  406.     if (command())
  407.            return(1);
  408.     if (c_token < num_tokens)    /* something after command */
  409.         if (equals(c_token, ";"))
  410.         c_token++;
  411.         else
  412.         int_error("';' expected", c_token);
  413.     }
  414.     return(0);
  415. }
  416.  
  417.  
  418.  
  419. command()
  420. {
  421.     FILE *fp, *lf_top();
  422.     int             i;
  423.     char            sv_file[MAX_LINE_LEN + 1];
  424. #if defined(__ZTC__)
  425.     unsigned dummy; /* it's a parameter needed for dos_setdrive */
  426. #endif
  427.     /* string holding name of save or load file */
  428.  
  429.     for (i = 0; i < MAX_NUM_VAR; i++)
  430.     c_dummy_var[i][0] = '\0';    /* no dummy variables */
  431.  
  432.     if (is_definition(c_token))
  433.     define();
  434.     else if (almost_equals(c_token, "h$elp") || equals(c_token, "?")) {
  435.     c_token++;
  436.     do_help();
  437.     } else if (almost_equals(c_token, "test")) {
  438.     c_token++;
  439.     test_term();
  440.     } else if (almost_equals(c_token, "scr$eendump")) {
  441.     c_token++;
  442. #ifdef _Windows
  443.     screen_dump();
  444. #else
  445.     fputs("screendump not implemented\n",stderr);
  446. #endif
  447.     } else if (almost_equals(c_token, "pa$use")) {
  448.     struct value    a;
  449.     int             stime, text = 0;
  450.     char            buf[MAX_LINE_LEN + 1];
  451.  
  452.     c_token++;
  453.     stime = (int) real(const_express(&a));
  454.     buf[0]='\0';
  455.     if (!(END_OF_COMMAND)) {
  456.         if (!isstring(c_token))
  457.         int_error("expecting string", c_token);
  458.         else {
  459.         quotel_str(buf, c_token);
  460. #ifdef _Windows
  461.         if (stime>=0)
  462. #endif
  463. #ifdef OS2
  464.                 if( strcmp(term_tbl[term].name, "pm" )!=0 || stime >=0 )
  465. #endif
  466.         (void) fprintf(stderr, "%s", buf);
  467.         text = 1;
  468.         }
  469.     }
  470.     if (stime < 0)
  471. #ifdef _Windows
  472.         {
  473.             if (!Pause(buf))
  474.                  longjmp(env, TRUE); /* bail out to command line */
  475.          }
  476. #else
  477. #ifdef OS2
  478.         if( strcmp(term_tbl[term].name, "pm" )==0 && stime < 0 )
  479.         {
  480.             int rc ;
  481.             if( (rc=PM_pause( buf ))==0 ) longjmp(env,TRUE) ;
  482.             else if( rc==2 ) { 
  483.         (void) fprintf(stderr, "%s", buf);
  484.         text = 1;
  485.                 (void) fgets(buf, MAX_LINE_LEN, stdin);
  486.                 }
  487.         }
  488. #else
  489.         (void) fgets(buf, MAX_LINE_LEN, stdin);
  490.     /* Hold until CR hit. */
  491. #endif /*OS2*/
  492. #endif
  493. #ifdef __ZTC__
  494.     if (stime > 0)
  495.         usleep((unsigned long) stime);
  496. #else
  497.     if (stime > 0)
  498.         sleep((unsigned int) stime);
  499. #endif
  500.     if (text != 0 && stime >= 0)
  501.         (void) fprintf(stderr, "\n");
  502.     c_token++;
  503.     screen_ok = FALSE;
  504.     } else if (almost_equals(c_token, "pr$int")) {
  505.     struct value    a;
  506.  
  507.     c_token++;
  508.     (void) const_express(&a);
  509.     (void) putc('\t', stderr);
  510.     disp_value(stderr, &a);
  511.     (void) putc('\n', stderr);
  512.     screen_ok = FALSE;
  513.     } else if (almost_equals(c_token, "p$lot")) {
  514.     plot_token = c_token++;
  515. #ifdef _Windows
  516.     SetCursor(LoadCursor((HINSTANCE)NULL, IDC_WAIT));
  517. #endif
  518.     plotrequest();
  519. #ifdef _Windows
  520.     SetCursor(LoadCursor((HINSTANCE)NULL, IDC_ARROW));
  521. #endif
  522.     } else if (almost_equals(c_token, "sp$lot")) {
  523.     plot_token = c_token++;
  524. #ifdef _Windows
  525.     SetCursor(LoadCursor((HINSTANCE)NULL, IDC_WAIT));
  526. #endif
  527.     plot3drequest();
  528. #ifdef _Windows
  529.     SetCursor(LoadCursor((HINSTANCE)NULL, IDC_ARROW));
  530. #endif
  531.     } else if (almost_equals(c_token, "rep$lot")) {
  532.     if (replot_line[0] == '\0')
  533.         int_error("no previous plot", c_token);
  534.     c_token++;
  535. #ifdef _Windows
  536.     SetCursor(LoadCursor((HINSTANCE)NULL, IDC_WAIT));
  537. #endif
  538.     replotrequest();
  539. #ifdef _Windows
  540.     SetCursor(LoadCursor((HINSTANCE)NULL, IDC_ARROW));
  541. #endif
  542.     } else if (almost_equals(c_token, "se$t"))
  543.     set_command();
  544.     else if (almost_equals(c_token, "sh$ow"))
  545.     show_command();
  546.     else if (almost_equals(c_token, "cl$ear")) {
  547.     if (!term_init) {
  548.         (*term_tbl[term].init) ();
  549.         term_init = TRUE;
  550.     }
  551.     (*term_tbl[term].graphics) ();
  552.     (*term_tbl[term].text) ();
  553.     (void) fflush(outfile);
  554.     screen_ok = FALSE;
  555.     c_token++;
  556.     } else if (almost_equals(c_token, "she$ll")) {
  557.     do_shell();
  558.     screen_ok = FALSE;
  559.     c_token++;
  560.     } else if (almost_equals(c_token, "sa$ve")) {
  561.     if (almost_equals(++c_token, "f$unctions")) {
  562.         if (!isstring(++c_token))
  563.         int_error("expecting filename", c_token);
  564.         else {
  565.         quote_str(sv_file, c_token);
  566.         save_functions(fopen(sv_file, "w"));
  567.         }
  568.     } else if (almost_equals(c_token, "v$ariables")) {
  569.         if (!isstring(++c_token))
  570.         int_error("expecting filename", c_token);
  571.         else {
  572.         quote_str(sv_file, c_token);
  573.         save_variables(fopen(sv_file, "w"));
  574.         }
  575.     } else if (almost_equals(c_token, "s$et")) {
  576.         if (!isstring(++c_token))
  577.         int_error("expecting filename", c_token);
  578.         else {
  579.         quote_str(sv_file, c_token);
  580.         save_set(fopen(sv_file, "w"));
  581.         }
  582.     } else if (isstring(c_token)) {
  583.         quote_str(sv_file, c_token);
  584.         save_all(fopen(sv_file, "w"));
  585.     } else {
  586.         int_error(
  587.              "filename or keyword 'functions', 'variables', or 'set' expected",
  588.              c_token);
  589.     }
  590.     c_token++;
  591.     } else if (almost_equals(c_token, "l$oad")) {
  592.     if (!isstring(++c_token))
  593.         int_error("expecting filename", c_token);
  594.     else {
  595.         quote_str(sv_file, c_token);
  596.         load_file(fp=fopen(sv_file, "r"), sv_file);
  597.         /* input_line[] and token[] now destroyed! */
  598.         c_token = num_tokens = 0;
  599.     }
  600.     } else if (almost_equals(c_token,"rer$ead")) {
  601.             fp = lf_top();
  602.             if (fp != (FILE *)NULL) rewind(fp);
  603.             c_token++;
  604.     } else if (almost_equals(c_token, "cd")) {
  605.     if (!isstring(++c_token))
  606.         int_error("expecting directory name", c_token);
  607.     else {
  608.         quotel_str(sv_file, c_token);
  609. #if defined(MSDOS) || defined(_Windows) || defined(ATARI) || defined(DOS386)
  610.         if (!((strlen(sv_file)==2) && isalpha(sv_file[0]) && (sv_file[1]==':')))
  611. #endif
  612.         if (chdir(sv_file)) {
  613.         int_error("Can't change to this directory", c_token);
  614.         }
  615. #if defined(MSDOS) || defined(_Windows) || defined(ATARI) || defined(DOS386)
  616.         if (isalpha(sv_file[0]) && (sv_file[1]==':')) {
  617. #ifdef ATARI
  618.         (void)Dsetdrv(toupper(sv_file[0])-'A');
  619. #endif
  620.  
  621. #if defined(__ZTC__)
  622.       (void)dos_setdrive(toupper(sv_file[0]) - 'A' + 1, &dummy);
  623. #endif
  624.  
  625. #if defined(MSDOS) && defined(__EMX__)
  626.         (void)_chdrive(toupper(sv_file[0]));
  627. #endif
  628. #if defined(__MSC__)
  629.         (void)_chdrive(toupper(sv_file[0])-'A');
  630. #endif
  631. #if (defined(MSDOS) || defined(_Windows)) && defined(__TURBOC__)
  632.         (void) setdisk(toupper(sv_file[0])-'A');
  633. #endif
  634. #ifdef DJGPP
  635.         { union REGS r;
  636.             r.h.ah = 0x0e;
  637.             r.x.dx = toupper(sv_file[0])-'A';
  638.             intdos(&r, &r);
  639.         }
  640. #endif
  641.         }
  642. #endif
  643.         c_token++;
  644.     }
  645.     } else if (almost_equals(c_token, "pwd")) {
  646. #if defined(unix) && !defined(hpux)
  647. #ifdef GETCWD
  648.     (void) getcwd(sv_file, MAX_ID_LEN);    /* some Unix's use getcwd */
  649. #else
  650.     (void) getwd(sv_file);    /* most Unix's use getwd */
  651. #endif
  652. #else
  653. #ifdef __EMX__
  654.     (void) _getcwd2(sv_file, MAX_ID_LEN);
  655. #else
  656.     /* Turbo C and VMS have getcwd() */
  657.     (void) getcwd(sv_file, MAX_ID_LEN);
  658. #endif
  659. #endif
  660. #ifdef DJGPP
  661.     { union REGS r;
  662.         r.h.ah = 0x19;
  663.         intdos(&r, &r);
  664.         fprintf(stderr, "%c:", r.h.al + 'a');
  665.     }
  666. #endif
  667.     fprintf(stderr, "%s\n", sv_file);
  668.     c_token++;
  669.     } else if (almost_equals(c_token, "ex$it") ||
  670.            almost_equals(c_token, "q$uit")) {
  671.     return(1);
  672.     } else if (!equals(c_token, ";")) {    /* null statement */
  673.     int_error("invalid command", c_token);
  674.     }
  675.     return(0);
  676. }
  677.  
  678. replotrequest()
  679. {
  680.     char            str[MAX_LINE_LEN + 1];
  681.     if (equals(c_token, "["))
  682.     int_error("cannot set range with replot", c_token);
  683.     if (!END_OF_COMMAND) {
  684.     capture(str, c_token, num_tokens - 1);
  685.     if ((strlen(str) + strlen(replot_line)) <= MAX_LINE_LEN - 1) {
  686.         (void) strcat(replot_line, ",");
  687.         (void) strcat(replot_line, str);
  688.     } else {
  689.         int_error("plot line too long with replot arguments", c_token);
  690.     }
  691.     }
  692.     (void) strcpy(input_line, replot_line);
  693.     screen_ok = FALSE;
  694.     num_tokens = scanner(input_line);
  695.     c_token = 1;        /* skip the 'plot' part */
  696.     is_3d_plot ? plot3drequest() : plotrequest();
  697. }
  698.  
  699.  
  700. plotrequest()
  701. /*
  702.  * In the parametric case we can say plot [a= -4:4] [-2:2] [-1:1] sin(a),a**2
  703.  * while in the non-parametric case we would say only plot [b= -2:2] [-1:1]
  704.  * sin(b)
  705.  */
  706. {
  707.     TBOOLEAN         changed;
  708.     int             dummy_token = -1;
  709.  
  710.     is_3d_plot = FALSE;
  711.  
  712.     if (parametric && strcmp(dummy_var[0], "u") == 0)
  713.     strcpy(dummy_var[0], "t");
  714.  
  715.     autoscale_lt = autoscale_t;
  716.     autoscale_lx = autoscale_x;
  717.     autoscale_ly = autoscale_y;
  718.  
  719.     if (!term)            /* unknown */
  720.     int_error("use 'set term' to set terminal type first", c_token);
  721.  
  722.     if (equals(c_token, "[")) {
  723.     c_token++;
  724.     if (isletter(c_token)) {
  725.         if (equals(c_token + 1, "=")) {
  726.         dummy_token = c_token;
  727.         c_token += 2;
  728.         } else {
  729.         /* oops; probably an expression with a variable. */
  730.         /* Parse it as an xmin expression. */
  731.         /* used to be: int_error("'=' expected",c_token); */
  732.         }
  733.     }
  734.     changed = parametric ? load_range(&tmin, &tmax) : load_range(&xmin, &xmax);
  735.     if (!equals(c_token, "]"))
  736.         int_error("']' expected", c_token);
  737.     c_token++;
  738.     if (changed) {
  739.         if (parametric)
  740.         autoscale_lt = FALSE;
  741.         else
  742.         autoscale_lx = FALSE;
  743.     }
  744.     }
  745.     if (parametric && equals(c_token, "[")) {    /* set optional x ranges */
  746.     c_token++;
  747.     changed = load_range(&xmin, &xmax);
  748.     if (!equals(c_token, "]"))
  749.         int_error("']' expected", c_token);
  750.     c_token++;
  751.     if (changed)
  752.         if(parametric)
  753.             autoscale_lt = FALSE;
  754.         else
  755.                 autoscale_lx = FALSE;
  756.     }
  757.     if (equals(c_token, "[")) {    /* set optional y ranges */
  758.     c_token++;
  759.     changed = load_range(&ymin, &ymax);
  760.     if (!equals(c_token, "]"))
  761.         int_error("']' expected", c_token);
  762.     c_token++;
  763.     if (changed)
  764.         autoscale_ly = FALSE;
  765.     }
  766.     /* use the default dummy variable unless changed */
  767.     if (dummy_token >= 0)
  768.     copy_str(c_dummy_var[0], dummy_token);
  769.     else
  770.     (void) strcpy(c_dummy_var[0], dummy_var[0]);
  771.  
  772.     eval_plots();
  773. }
  774.  
  775. plot3drequest()
  776. /*
  777.  * in the parametric case we would say splot [u= -Pi:Pi] [v= 0:2*Pi] [-1:1]
  778.  * [-1:1] [-1:1] sin(v)*cos(u),sin(v)*cos(u),sin(u) in the non-parametric
  779.  * case we would say only splot [x= -2:2] [y= -5:5] sin(x)*cos(y)
  780.  * 
  781.  */
  782. {
  783.     TBOOLEAN         changed;
  784.     int             dummy_token0 = -1, dummy_token1 = -1;
  785.  
  786.     is_3d_plot = TRUE;
  787.  
  788.     if (parametric && strcmp(dummy_var[0], "t") == 0) {
  789.     strcpy(dummy_var[0], "u");
  790.     strcpy(dummy_var[1], "v");
  791.     }
  792.     autoscale_lx = autoscale_x;
  793.     autoscale_ly = autoscale_y;
  794.     autoscale_lz = autoscale_z;
  795.  
  796.     if (!term)            /* unknown */
  797.     int_error("use 'set term' to set terminal type first", c_token);
  798.  
  799.     if (equals(c_token, "[")) {
  800.     c_token++;
  801.     if (isletter(c_token)) {
  802.         if (equals(c_token + 1, "=")) {
  803.         dummy_token0 = c_token;
  804.         c_token += 2;
  805.         } else {
  806.         /* oops; probably an expression with a variable. */
  807.         /* Parse it as an xmin expression. */
  808.         /* used to be: int_error("'=' expected",c_token); */
  809.         }
  810.     }
  811.     changed = parametric ? load_range(&umin, &umax) : load_range(&xmin, &xmax);
  812.     if (!equals(c_token, "]"))
  813.         int_error("']' expected", c_token);
  814.     c_token++;
  815.     if (changed)
  816.         if(parametric) 
  817.             autoscale_lu = FALSE;
  818.         else
  819.             autoscale_lx = FALSE;
  820.     }
  821.     if (equals(c_token, "[")) {
  822.     c_token++;
  823.     if (isletter(c_token)) {
  824.         if (equals(c_token + 1, "=")) {
  825.         dummy_token1 = c_token;
  826.         c_token += 2;
  827.         } else {
  828.         /* oops; probably an expression with a variable. */
  829.         /* Parse it as an xmin expression. */
  830.         /* used to be: int_error("'=' expected",c_token); */
  831.         }
  832.     }
  833.     changed = parametric ? load_range(&vmin, &vmax) : load_range(&ymin, &ymax);
  834.     if (!equals(c_token, "]"))
  835.         int_error("']' expected", c_token);
  836.     c_token++;
  837.     if (changed)
  838.         if(parametric) 
  839.             autoscale_lv = FALSE;
  840.         else
  841.             autoscale_ly = FALSE;
  842.     }
  843.     if (equals(c_token, "[")) {    /* set optional x (parametric) or z ranges */
  844.     c_token++;
  845.     changed = parametric ? load_range(&xmin, &xmax) : load_range(&zmin, &zmax);
  846.     if (!equals(c_token, "]"))
  847.         int_error("']' expected", c_token);
  848.     c_token++;
  849.     if (changed)
  850.         if(parametric) 
  851.             autoscale_lx = FALSE;
  852.         else
  853.             autoscale_lz = FALSE;
  854.     }
  855.     if (equals(c_token, "[")) {    /* set optional y ranges */
  856.     c_token++;
  857.     changed = load_range(&ymin, &ymax);
  858.     if (!equals(c_token, "]"))
  859.         int_error("']' expected", c_token);
  860.     c_token++;
  861.     if (changed)
  862.         autoscale_ly = FALSE;
  863.     }
  864.     if (equals(c_token, "[")) {    /* set optional z ranges */
  865.     c_token++;
  866.     changed = load_range(&zmin, &zmax);
  867.     if (!equals(c_token, "]"))
  868.         int_error("']' expected", c_token);
  869.     c_token++;
  870.     if (changed)
  871.         autoscale_lz = FALSE;
  872.     }
  873.     /* use the default dummy variable unless changed */
  874.     if (dummy_token0 >= 0)
  875.     copy_str(c_dummy_var[0], dummy_token0);
  876.     else
  877.     (void) strcpy(c_dummy_var[0], dummy_var[0]);
  878.  
  879.     if (dummy_token1 >= 0)
  880.     copy_str(c_dummy_var[1], dummy_token1);
  881.     else
  882.     (void) strcpy(c_dummy_var[1], dummy_var[1]);
  883.  
  884.     eval_3dplots();
  885. }
  886.  
  887.  
  888. define()
  889. {
  890.     register int    start_token;/* the 1st token in the function definition */
  891.     register struct udvt_entry *udv;
  892.     register struct udft_entry *udf;
  893.  
  894.     if (equals(c_token + 1, "(")) {
  895.     /* function ! */
  896.     int             dummy_num = 0;
  897.     start_token = c_token;
  898.     do {
  899.         c_token += 2;    /* skip to the next dummy */
  900.         copy_str(c_dummy_var[dummy_num++], c_token);
  901.     } while (equals(c_token + 1, ",") && (dummy_num < MAX_NUM_VAR));
  902.     if (equals(c_token + 1, ","))
  903.         int_error("function contains too many parameters", c_token + 2);
  904.     c_token += 3;        /* skip (, dummy, ) and = */
  905.     if (END_OF_COMMAND)
  906.         int_error("function definition expected", c_token);
  907.     udf = dummy_func = add_udf(start_token);
  908.     if (udf->at)        /* already a dynamic a.t. there */
  909.         free((char *) udf->at);    /* so free it first */
  910.     if ((udf->at = perm_at()) == (struct at_type *) NULL)
  911.         int_error("not enough memory for function", start_token);
  912.     m_capture(&(udf->definition), start_token, c_token - 1);
  913.     } else {
  914.     /* variable ! */
  915.     start_token = c_token;
  916.     c_token += 2;
  917.     udv = add_udv(start_token);
  918.     (void) const_express(&(udv->udv_value));
  919.     udv->udv_undef = FALSE;
  920.     }
  921. }
  922.  
  923. get_data(this_plot)
  924.     struct curve_points *this_plot;
  925. {
  926.     register int    i, j, l_num, datum;
  927.     int fcol[5], scol[5], ncol[5], prevmin, col;
  928.     char  format[MAX_LINE_LEN + 1], data_file[MAX_LINE_LEN + 1],
  929.           line[MAX_LINE_LEN + 1];
  930.     /* conversion variables */
  931.     int n, m, linestat, using;
  932.     char *s;
  933.     double val[5], v[5];
  934.     float fval[5];    /* for use in sscanf */
  935.  
  936.     /* close forgotten input file (in case of a syntax error) */
  937.     if( data_fp ) {
  938. #if defined(unix) || defined(PIPES)
  939.         if (pipe_open) {
  940.             (void) pclose(data_fp);
  941.             pipe_open = FALSE;
  942.         } else
  943. #endif /* unix || PIPES */
  944.         (void) fclose(data_fp);
  945.         data_fp=NULL;
  946.     }
  947.  
  948.     quotel_str(data_file, c_token);
  949.     this_plot->plot_type = DATA;
  950. /*    if (parametric)
  951.         int_error("Parametric data files not yet implemented", NO_CARET);
  952. */
  953. #if defined(unix) || defined(PIPES)
  954.     if (*data_file == '<') {
  955.         if ((data_fp = popen(data_file + 1, "r")) == (FILE *) NULL)
  956.             os_error("cannot create pipe for data", c_token);
  957.         else
  958.             pipe_open = TRUE;
  959.     } else
  960. #endif /* unix || PIPES */
  961.     if ((data_fp = fopen(data_file, "r")) == (FILE *) NULL)
  962.         os_error("can't open data file", c_token);
  963.  
  964.     format[0] = '\0';
  965.     for (i=0; i<5; i++)
  966.         fcol[i] = i+1;
  967.  
  968.     using = 0;
  969.     c_token++;            /* skip data file name */
  970.  
  971.     /* jev -- support for passing data from file thru user function */
  972.     if (almost_equals(c_token, "thru$")) {
  973.         c_token++;
  974.         if (ydata_func.at)
  975.             free(ydata_func.at);
  976.         dummy_func = &ydata_func;
  977.         ydata_func.at = perm_at();
  978.     } else {
  979.         if (ydata_func.at)
  980.             free(ydata_func.at);
  981.         ydata_func.at = NULL;
  982.     }
  983.  
  984.     if (almost_equals(c_token,"u$sing")) {
  985.         using = 1;
  986.         c_token++;      /* skip "using" */
  987.             
  988.         if (!END_OF_COMMAND && !isstring(c_token)) {
  989.             struct value a;
  990.             for (i=0; i<5; i++)
  991.                 fcol[i] = -1;
  992.             fcol[0] = fcol[1] = (int)magnitude(const_express(&a));
  993.             for (i=1; equals(c_token,":") && i<5; i++) {
  994.                 c_token++;      /* skip ":" */
  995.                 fcol[i] = (int)magnitude(const_express(&a));
  996.             }
  997.             if (i==1)  /* only y column given */
  998.                 fcol[0] = 1;
  999.         }
  1000.  
  1001.         if (!END_OF_COMMAND && isstring(c_token)) {
  1002.             quotel_str(format, c_token);
  1003.             c_token++;    /* skip format */
  1004.         }
  1005.     }
  1006.     
  1007.     /* sort fcol[] into scol[] removing duplicates */
  1008.     prevmin = 0;
  1009.     for (i=0; i<5; i++) {
  1010.         col = 10000;
  1011.         for (j=0; j<5; j++)
  1012.             if ((fcol[j]>prevmin) && (fcol[j]<col))
  1013.                 col = fcol[j];
  1014.         if (col <10000)
  1015.                prevmin = scol[i] = col;
  1016.         else
  1017.             scol[i] = 0;
  1018.     }
  1019.     /* normalise fcol[] into ncol[] */
  1020.     for (i=0; i<5; i++) {
  1021.         if (fcol[i] > 0)
  1022.             for (j=0; j<5; j++) {
  1023.                 if (fcol[i] == scol[j])
  1024.                     ncol[i] = j+1;
  1025.             }
  1026.         else if (fcol[i] == 0)
  1027.             ncol[i] = 0;
  1028.         else
  1029.             ncol[i] = -1;
  1030.     }
  1031.     /* set col to highest column number */
  1032.     col = 0;
  1033.     for (i=0; i<5; i++) 
  1034.         if (fcol[i]>col) col=fcol[i];
  1035.  
  1036.     l_num = 0;
  1037.     datum = 0;
  1038.     i = 0;
  1039.     while (fgets(line, MAX_LINE_LEN, data_fp) != (char *) NULL) {
  1040.         l_num++;
  1041.         if (is_comment(line[0]))
  1042.             continue;        /* ignore comments */
  1043.         if (i >= this_plot->p_max) {
  1044.             /*
  1045.              * overflow about to occur. Extend size of points[] array. We
  1046.              * either double the size, or add 1000 points, whichever is a
  1047.              * smaller increment. Note i=p_max.
  1048.              */
  1049.             cp_extend(this_plot, i + (i < 1000 ? i : 1000));
  1050.         }
  1051.         if (!line[1]) {        /* is it blank line ? */
  1052.             /* break in data, make next point undefined */
  1053.             this_plot->points[i].type = UNDEFINED;
  1054.             i++;
  1055.             continue;
  1056.         }
  1057.         if (strlen(format) != 0) {
  1058.             /* use old sscanf if a format string was given */
  1059.             m = sscanf(line, format, &fval[0], &fval[1], &fval[2], &fval[3], &fval[4]);
  1060.             for (n=0; n<5; n++)        /* convert floats from sscanf to double */
  1061.                 val[n] = (double)fval[n];
  1062.  
  1063.             for (j=0; j<5 && fcol[j]>=0 && fcol[j]-1<m ; j++)
  1064.                 if (fcol[j])
  1065.                     v[j] = val[fcol[j]-1];
  1066.                 else
  1067.                     v[j] = datum;    /* using 0:n */
  1068.         }
  1069.         else {
  1070.             /* implement our own sscanf that skips lines with invalid data 
  1071.              * if a using statement was given */
  1072.             /* convert the array to its constituents */
  1073.             for(n=0; n<5; n++)            /* wipe the array */
  1074.                 val[n] = 0.0;
  1075.             n=0;                        /* n is column number */
  1076.             m=0;                        /* m is number of values read */
  1077.             linestat = 1;                /* linestat: 1 OK 2 bad value 0 EOL */
  1078.             s = line;
  1079.             while ((linestat == 1) && (n<col)) {
  1080.                 while (isspace(*s)) s++;
  1081.                 if (*s == '\0') {
  1082.                     linestat = 0;
  1083.                     break;
  1084.                 }
  1085.                 n++;
  1086.                 if (n == scol[m]) {
  1087.                     if (isdigit(*s) || *s=='-' || *s=='+' || *s=='.') {
  1088.                         val[m] = atof(s);
  1089.                         m++;
  1090.                     }
  1091.                     else
  1092.                         linestat = 2;    /* abort the line non-digit in req loc */
  1093.                 }
  1094.                 while ((!isspace(*s)) && (*s != '\0')) s++;
  1095.             }
  1096.  
  1097.             if (using && (linestat == 2))
  1098.                 continue;    /* skip lines where a using pattern is present and not met */
  1099.  
  1100.             for (j=0; j<5 && ncol[j]>=0 && ncol[j]-1<m ; j++)
  1101.                 if (ncol[j])
  1102.                     v[j] = val[ncol[j]-1];
  1103.                 else
  1104.                     v[j] = datum;    /* using 0:n */
  1105.         }
  1106.  
  1107.         switch (j) {
  1108.             case 1: {        /* only one number */
  1109.                 /* x is index, assign number to y */
  1110.                 v[1]=v[0];
  1111.                 v[0]=datum;
  1112.                 /* nobreak */
  1113.             }
  1114.             case 2: {        /* x, y */
  1115.                 /* ylow and yhigh are same as y */
  1116.                 datum++;
  1117.                 store2d_point(this_plot, i++, v[0], v[1], v[1], v[1], -1.0);
  1118.                 break;
  1119.             }
  1120.             case 3: {        /* x, y, ydelta */
  1121.                 /* ydelta is in the ylow variable */
  1122.                 datum++;
  1123.                 store2d_point(this_plot, i++, v[0], v[1], v[1]-v[2], v[1]+v[2], -1.0);
  1124.                 break;
  1125.             }
  1126.             case 4: {        /* x, y, ylow, yhigh */
  1127.                 datum++;
  1128.                 store2d_point(this_plot, i++, v[0], v[1], v[2], v[3], -1.0);
  1129.                 break;
  1130.             }
  1131.             case 5: {        /* x, y, ylow, yhigh, width */
  1132.                 datum++;
  1133.                 store2d_point(this_plot, i++, v[0], v[1], v[2], v[3], v[4]);
  1134.                 break;
  1135.             }
  1136.             default: {
  1137.                 (void) sprintf(line, "bad data on line %d", l_num);
  1138.                 /* close file before exiting to command level */
  1139. #if defined(unix) || defined(PIPES)
  1140.                 if (pipe_open) {
  1141.                     (void) pclose(data_fp);
  1142.                     pipe_open = FALSE;
  1143.                 } else
  1144. #endif /* unix || PIPES */
  1145.                     (void) fclose(data_fp);
  1146.                 data_fp=NULL;
  1147.                 int_error(line, c_token);
  1148.             }
  1149.         }
  1150.     }
  1151.     this_plot->p_count = i;
  1152.     cp_extend(this_plot, i);    /* shrink to fit */
  1153.  
  1154. #if defined(unix) || defined(PIPES)
  1155.     if (pipe_open) {
  1156.         (void) pclose(data_fp);
  1157.         pipe_open = FALSE;
  1158.     } else
  1159. #endif /* unix || PIPES */
  1160.     (void) fclose(data_fp);
  1161.     data_fp=NULL;
  1162. }
  1163.  
  1164.  
  1165. /* called by get_data for each point */
  1166. store2d_point(this_plot, i, x, y, ylow, yhigh, width)
  1167.     struct curve_points *this_plot;
  1168.     int             i;        /* point number */
  1169.     double          x, y;
  1170.     double          ylow, yhigh;
  1171.     double          width;
  1172. {
  1173.     struct coordinate GPHUGE *cp = &(this_plot->points[i]);
  1174.  
  1175.     /* the easy part: */
  1176.     cp->type = INRANGE;
  1177.     cp->x = x;
  1178.     cp->y = y;
  1179.     cp->ylow = ylow;
  1180.     cp->yhigh = yhigh;
  1181.     cp->z = width;
  1182.  
  1183.     /* jev -- pass data values thru user-defined function */
  1184.     if (ydata_func.at) {
  1185.     struct value    val;
  1186.  
  1187.     (void) Gcomplex(&ydata_func.dummy_values[0], y, 0.0);
  1188.     evaluate_at(ydata_func.at, &val);
  1189.     cp->y = real(&val);
  1190.  
  1191.     (void) Gcomplex(&ydata_func.dummy_values[0], ylow, 0.0);
  1192.     evaluate_at(ydata_func.at, &val);
  1193.     cp->ylow = real(&val);
  1194.  
  1195.     (void) Gcomplex(&ydata_func.dummy_values[0], yhigh, 0.0);
  1196.     evaluate_at(ydata_func.at, &val);
  1197.     cp->yhigh = real(&val);
  1198.     }
  1199.     /* Adjust for log scale. */
  1200.     if (is_log_x) {
  1201.         cp->type = adjustlog(cp->type, &(cp->x), log_base_log_x);
  1202.         (void) adjustlog(cp->type, &(cp->z), log_base_log_z);
  1203.     }
  1204.     if (is_log_y) {
  1205.     cp->type = adjustlog(cp->type, &(cp->y), log_base_log_y);
  1206.     /* Note ylow,yhigh can't affect cp->type. */
  1207.     (void) adjustlog(cp->type, &(cp->ylow), log_base_log_y);
  1208.     (void) adjustlog(cp->type, &(cp->yhigh), log_base_log_y);
  1209.     }
  1210.     /* Now adjust the xrange, or declare the point out of range */
  1211.     /*
  1212.      * The yrange is handled later, once we know whether to include ylow,
  1213.      * yhigh in the calculation. See adjust_yrange()
  1214.      */
  1215.     if (cp->type == INRANGE)
  1216.     if (autoscale_lx || inrange(x, xmin, xmax)) {
  1217.         if (autoscale_lx) {
  1218.         if (x < xmin)
  1219.             xmin = x;
  1220.         if (x > xmax)
  1221.             xmax = x;
  1222.         }
  1223.     } else {
  1224.         cp->type = OUTRANGE;
  1225.     }
  1226. }
  1227.  
  1228.  
  1229. /*
  1230.  * Adjust for log scale: take the log of the second parameter, in place, if
  1231.  * possible. If not possible, return new type for point; if possible, then
  1232.  * return old type for point.  The log is taken to the base implicit in the
  1233.  * third parameter.
  1234.  */
  1235. static enum coord_type
  1236. adjustlog(type, val, log_base_log)
  1237.     enum coord_type type;
  1238.     coordval       *val;
  1239.     double          log_base_log;
  1240. {
  1241.     if (*val < 0.0) {
  1242.     return (UNDEFINED);
  1243.     } else if (*val == 0.0) {
  1244.     *val = -VERYLARGE;
  1245.     return (OUTRANGE);
  1246.     } else {
  1247.     *val = log(*val)/log_base_log;
  1248.     return (type);
  1249.     }
  1250. }
  1251.  
  1252.  
  1253. /* now adjust the yrange, or declare the point out of range */
  1254. /* this does all points in a curve */
  1255. adjust_yrange(curve)
  1256.     struct curve_points *curve;
  1257. {
  1258.     TBOOLEAN         ebars = (curve->plot_style == ERRORBARS);
  1259.     int             npoints = curve->p_count;    /* number of points */
  1260.     coordval        y, ylow, yhigh;    /* one point value */
  1261.     struct coordinate GPHUGE *cp;    /* one coordinate */
  1262.     int             i;        /* index into points */
  1263.  
  1264.     for (i = 0; i < npoints; i++) {
  1265.     cp = &(curve->points[i]);
  1266.     if (cp->type == INRANGE) {
  1267.         y = is_log_y ? pow(base_log_y, cp->y) : cp->y;
  1268.         if ((autoscale_ly ||
  1269.         /*
  1270.          * inrange((is_log_y ? pow(base_log_y, y) : y), ymin, ymax) ||
  1271.          */
  1272.          inrange((y), ymin, ymax) ||
  1273.          polar)) {
  1274.         if (autoscale_ly) {
  1275.             if (y < ymin)
  1276.             ymin = y;
  1277.             if (y > ymax)
  1278.             ymax = y;
  1279.             if (ebars) {
  1280.             ylow = is_log_y ? pow(base_log_y, cp->ylow) : cp->ylow;
  1281.             yhigh = is_log_y ? pow(base_log_y, cp->yhigh) : cp->yhigh;
  1282.             if (ylow < ymin)
  1283.                 ymin = ylow;
  1284.             if (ylow > ymax)
  1285.                 ymax = ylow;
  1286.             if (yhigh < ymin)
  1287.                 ymin = yhigh;
  1288.             if (yhigh > ymax)
  1289.                 ymax = yhigh;
  1290.             }
  1291.         }
  1292.         } else {
  1293.         cp->type = OUTRANGE;
  1294.         }
  1295.     }
  1296.     }
  1297. }
  1298.  
  1299. grid_nongrid_data(this_plot)
  1300. struct surface_points *this_plot;
  1301. {
  1302.     int i, j, k;
  1303.     double x, y, z, w, dx, dy, xmin, xmax, ymin, ymax;
  1304.     struct iso_curve *old_iso_crvs = this_plot->iso_crvs;
  1305.     struct iso_curve *icrv, *oicrv, *oicrvs;
  1306.  
  1307.     /* Compute XY bounding box on the original data. */
  1308.     xmin = xmax = old_iso_crvs->points[0].x;
  1309.     ymin = ymax = old_iso_crvs->points[0].y;
  1310.     for (icrv = old_iso_crvs; icrv != NULL; icrv = icrv->next) {
  1311.     struct coordinate GPHUGE *points = icrv->points;
  1312.  
  1313.     for (i = 0; i < icrv->p_count; i++, points++) {
  1314.         if (xmin > points->x)
  1315.         xmin = points->x;
  1316.         if (xmax < points->x)
  1317.         xmax = points->x;
  1318.         if (ymin > points->y)
  1319.         ymin = points->y;
  1320.         if (ymax < points->y)
  1321.         ymax = points->y;
  1322.     }
  1323.     }
  1324.  
  1325.     dx = (xmax - xmin) / (dgrid3d_row_fineness - 1);
  1326.     dy = (ymax - ymin) / (dgrid3d_row_fineness - 1);
  1327.  
  1328.     /* Create the new grid structure, and compute the low pass filtering from
  1329.      * non grid to grid structure.
  1330.      */
  1331.     this_plot->iso_crvs = NULL;
  1332.     this_plot->num_iso_read = dgrid3d_col_fineness;
  1333.     this_plot->has_grid_topology = TRUE;
  1334.     for (i = 0, x = xmin; i < dgrid3d_col_fineness; i++, x += dx) {
  1335.     struct coordinate GPHUGE *points;
  1336.  
  1337.     icrv = iso_alloc(dgrid3d_row_fineness + 1);
  1338.     icrv->p_count = dgrid3d_row_fineness;
  1339.     icrv->next = this_plot->iso_crvs;
  1340.     this_plot->iso_crvs = icrv;
  1341.     points = icrv->points;
  1342.  
  1343.     for (j = 0, y = ymin; j < dgrid3d_row_fineness; j++, y += dy, points++) {
  1344.         z = w = 0.0;
  1345.  
  1346.         for (oicrv = old_iso_crvs; oicrv != NULL; oicrv = oicrv->next) {
  1347.         struct coordinate GPHUGE *opoints = oicrv->points;
  1348.         for (k = 0; k < oicrv->p_count; k++, opoints++) {
  1349.             double dist,
  1350.                dist_x = fabs( opoints->x - x ),
  1351.                dist_y = fabs( opoints->y - y );
  1352.  
  1353.             switch (dgrid3d_norm_value) {
  1354.             case 1:
  1355.                 dist = dist_x + dist_y;
  1356.                 break;
  1357.             case 2:
  1358.                 dist = dist_x * dist_x + dist_y * dist_y;
  1359.                 break;
  1360.             case 4:
  1361.                 dist = dist_x * dist_x + dist_y * dist_y;
  1362.                 dist *= dist;
  1363.                 break;
  1364.             case 8:
  1365.                 dist = dist_x * dist_x + dist_y * dist_y;
  1366.                 dist *= dist;
  1367.                 dist *= dist;
  1368.                 break;
  1369.             case 16:
  1370.                 dist = dist_x * dist_x + dist_y * dist_y;
  1371.                 dist *= dist;
  1372.                 dist *= dist;
  1373.                 dist *= dist;
  1374.                 break;
  1375.             default:
  1376.                 dist = pow( dist_x, dgrid3d_norm_value ) +
  1377.                    pow( dist_y, dgrid3d_norm_value );
  1378.                 break;
  1379.             }
  1380.  
  1381.             /* The weight of this point is inverse proportional
  1382.              * to the distance.
  1383.              */
  1384.             if ( dist == 0.0 )
  1385. #ifndef AMIGA_SC_6_1
  1386.             dist = VERYLARGE;
  1387. #else /* AMIGA_SC_6_1 */
  1388.             /* Multiplying VERYLARGE by opoints->z below
  1389.              * might yield Inf (i.e. a number that can't
  1390.              * be represented on the machine). This will
  1391.              * result in points->z being set to NaN. It's
  1392.              * better to have a pretty large number that is
  1393.              * also on the safe side... The numbers that are
  1394.              * read by gnuplot are float values anyway, so
  1395.              * they can't be bigger than FLT_MAX. So setting
  1396.              * dist to FLT_MAX^2 will make dist pretty large
  1397.              * with respect to any value that has been read. */
  1398.             dist = ((double)FLT_MAX)*((double)FLT_MAX);
  1399. #endif /* AMIGA_SC_6_1 */
  1400.             else
  1401.             dist = 1.0 / dist;
  1402.  
  1403.             z += opoints->z * dist;
  1404.             w += dist;
  1405.         }
  1406.         }
  1407.  
  1408.         points->x = x;
  1409.         points->y = y;
  1410.         points->z = z / w;
  1411.         points->type = INRANGE;
  1412.     }
  1413.     }
  1414.     
  1415.     /* Delete the old non grid data. */
  1416.     for (oicrvs = old_iso_crvs; oicrvs != NULL;) {
  1417.     oicrv = oicrvs;
  1418.     oicrvs = oicrvs->next;
  1419.     iso_free(oicrv);
  1420.     }
  1421. }
  1422.  
  1423. get_3ddata(this_plot)
  1424.     struct surface_points *this_plot;
  1425. {
  1426.     register int    i, j, l_num, xdatum, ydatum;
  1427.     float           x, y, z;
  1428.     char            data_file[MAX_LINE_LEN + 1], line[MAX_LINE_LEN + 1];
  1429.     char           *float_format = "%f", *float_skip = "%*f";
  1430.     static TBOOLEAN  only_z = FALSE, using_format = FALSE;
  1431.     static int      xcol = 1, ycol = 2, zcol = 3, index = -1;
  1432.     static char     format[MAX_LINE_LEN + 1];
  1433.     int            pt_in_iso_crv = 0, maxcol, num_col;
  1434.     enum XYZ_order_type {
  1435.     XYZ, YXZ, ZXY, XZY, ZYX, YZX, XY, YX
  1436.     }               xyz_order;
  1437.     struct iso_curve *this_iso;
  1438.  
  1439.     /* close forgotten file (in case of a syntax error) */
  1440.     if (data_fp && !more_data_fp) {
  1441. #if defined(unix) || defined(PIPES)
  1442.     if (pipe_open) {
  1443.         (void) pclose(data_fp);
  1444.         pipe_open = FALSE;
  1445.     } else
  1446. #endif /* unix || PIPES */
  1447.         (void) fclose(data_fp);
  1448.         data_fp=NULL;
  1449.         }
  1450.  
  1451.     quotel_str(data_file, c_token);
  1452.     this_plot->plot_type = DATA3D;
  1453.     this_plot->has_grid_topology = TRUE;
  1454.     if (!more_data_fp) {
  1455. #if defined(unix) || defined(PIPES)
  1456.     if (*data_file == '<') {
  1457.         if ((data_fp = popen(data_file + 1, "r")) == (FILE *) NULL)
  1458.         os_error("cannot create pipe; output not changed", c_token);
  1459.         else
  1460.         pipe_open = TRUE;
  1461.     } else
  1462. #endif /* unix || PIPES */
  1463.         if ((data_fp = fopen(data_file, "r")) == (FILE *) NULL)
  1464.         os_error("can't open data file", c_token);
  1465.  
  1466.     /* Initialize defualt values. */
  1467.     only_z = FALSE;
  1468.     using_format = FALSE;
  1469.     xcol = 1;
  1470.     ycol = 2;
  1471.     zcol = 3;
  1472.     index = -1;
  1473.     format[0] = '\0';
  1474.  
  1475.     c_token++;            /* skip data file name */
  1476.     if (almost_equals(c_token, "i$ndex")) {
  1477.         struct value a;
  1478.         c_token++;        /* skip "index" */
  1479.         index = (int) magnitude(const_express(&a));
  1480.     }
  1481.     if (almost_equals(c_token, "u$sing")) {
  1482.         c_token++;        /* skip "using" */
  1483.         if (!END_OF_COMMAND && !isstring(c_token)) {
  1484.         struct value a;
  1485.         zcol = (int) magnitude(const_express(&a));
  1486.         only_z = TRUE;
  1487.         if (equals(c_token, ":")) {
  1488.             c_token++;    /* skip ":" */
  1489.             only_z = FALSE;
  1490.             ycol = zcol;
  1491.             zcol = (int) magnitude(const_express(&a));
  1492.             if (equals(c_token, ":")) {
  1493.             c_token++;    /* skip ":" */
  1494.             xcol = ycol;
  1495.             ycol = zcol;
  1496.             zcol = (int) magnitude(const_express(&a));
  1497.             } else {
  1498.             if (mapping3d == MAP3D_CARTESIAN)
  1499.                 int_error("Must specify 1 or 3 columns", c_token);
  1500.             xcol = ycol;
  1501.             ycol = zcol;
  1502.             }
  1503.         }
  1504.         if (!only_z)
  1505.             if ((xcol == ycol) || (ycol == zcol) || (xcol == zcol))
  1506.             int_error("Columns must be distinct", c_token);
  1507.         }
  1508.         if (!END_OF_COMMAND && isstring(c_token)) {
  1509.         quotel_str(format, c_token);
  1510.         using_format = TRUE;
  1511.         c_token++;        /* skip format */
  1512.         }
  1513.     } else {
  1514.         if ( (only_z = !parametric) != FALSE)
  1515.         zcol = 1;
  1516.     }
  1517.     }
  1518.  
  1519.     switch (mapping3d) {
  1520.     case MAP3D_CARTESIAN:
  1521.     maxcol = (xcol > ycol) ? xcol : ycol;
  1522.     maxcol = (maxcol > zcol) ? maxcol : zcol;
  1523.     if (!only_z) {        /* Determine ordering of input columns */
  1524.         if (zcol == maxcol) {
  1525.         if (xcol < ycol)
  1526.             xyz_order = XYZ;    /* scanf(x,y,z) */
  1527.         else
  1528.             xyz_order = YXZ;    /* scanf(y,x,z) */
  1529.         } else if (ycol == maxcol) {
  1530.         if (xcol < zcol)
  1531.             xyz_order = XZY;    /* scanf(x,z,y) */
  1532.         else
  1533.             xyz_order = ZXY;    /* scanf(z,x,y) */
  1534.         } else {
  1535.         if (ycol < zcol)
  1536.             xyz_order = YZX;    /* scanf(y,z,x) */
  1537.         else
  1538.             xyz_order = ZYX;    /* scanf(z,y,x) */
  1539.         }
  1540.     }
  1541.     if (strlen(format) == 0) {
  1542.         if (only_z) {
  1543.         for (i = 1; i <= zcol; i++)
  1544.             if (i == zcol)
  1545.             (void) strcat(format, float_format);
  1546.             else
  1547.             (void) strcat(format, float_skip);
  1548.         } else {
  1549.         for (i = 1; i <= maxcol; i++)
  1550.             if ((i == xcol) || (i == ycol) || (i == zcol))
  1551.             (void) strcat(format, float_format);
  1552.             else
  1553.             (void) strcat(format, float_skip);
  1554.         }
  1555.     }
  1556.     break;
  1557.     case MAP3D_SPHERICAL:
  1558.     case MAP3D_CYLINDRICAL:
  1559.     if (only_z)
  1560.         int_error("Two or three columns for spherical/cylindrical coords.", c_token);
  1561.     maxcol = (xcol > ycol) ? xcol : ycol;
  1562.     maxcol = (maxcol > zcol) ? maxcol : zcol;
  1563.     xyz_order = (xcol < ycol) ? XY : YX;
  1564.     for (i = 1; i <= maxcol; i++)
  1565.         if ((i == xcol) || (i == ycol))
  1566.         (void) strcat(format, float_format);
  1567.         else
  1568.         (void) strcat(format, float_skip);
  1569.     }
  1570.  
  1571.     l_num = 0;
  1572.     xdatum = 0;
  1573.     ydatum = 0;
  1574.     this_plot->num_iso_read = 0;
  1575.     this_plot->has_grid_topology = TRUE;
  1576.     if (this_plot->iso_crvs != NULL) {
  1577.     struct iso_curve *icrv, *icrvs = this_plot->iso_crvs;
  1578.  
  1579.     while (icrvs) {
  1580.         icrv = icrvs;
  1581.         icrvs = icrvs->next;
  1582.         iso_free(icrv);
  1583.     }
  1584.     this_plot->iso_crvs = NULL;
  1585.     }
  1586.     if (!more_data_fp && is_binary_file(data_fp)) {    /* MOD--RKC */
  1587. #if defined(MSDOS)||defined(ATARI)||defined(OS2)||defined(_Windows)||defined(DOS386)
  1588.     /* file must be opened with binary flag. the old cr/lf problem again */
  1589. #ifdef PIPES
  1590.     if( pipe_open ) {
  1591.       pclose(data_fp);
  1592.       data_fp=NULL;
  1593.       pipe_open=FALSE;
  1594.       int_error("binary data from pipes is not implemented", NO_CARET);
  1595.     }
  1596. #endif
  1597.     data_fp = freopen(data_file, "rb", data_fp);
  1598. #endif
  1599.     xdatum = get_binary_data(this_plot, data_fp, &this_iso);
  1600.     } else {
  1601.     int last_line_blank = FALSE;
  1602.  
  1603.     more_data_fp = FALSE;
  1604.  
  1605.     this_iso = iso_alloc(samples);
  1606.  
  1607.     if (index > 0) { /* Skip data meshes until mesh index is reached. */
  1608.         int i = index;
  1609.  
  1610.         while (i--) {
  1611.         while (fgets(line, MAX_LINE_LEN, data_fp) != (char *) NULL) {
  1612.             l_num++;
  1613.  
  1614.             if (!line[1]) {    /* is it blank line ? */
  1615.             if (last_line_blank) /* Two consecutive blanks. */
  1616.                 break;
  1617.             last_line_blank = TRUE;
  1618.             }
  1619.             else
  1620.             last_line_blank = FALSE;
  1621.         }
  1622.         if (feof(data_fp))
  1623.             int_error("mesh index overflow", NO_CARET);
  1624.         }
  1625.     }   /* end of index skip */
  1626.     
  1627.     last_line_blank = FALSE;
  1628.  
  1629.     while (fgets(line, MAX_LINE_LEN, data_fp) != (char *) NULL) {
  1630.         l_num++;
  1631.         if (is_comment(line[0]))
  1632.         continue;    /* ignore comments */
  1633.         if (!line[1]) {    /* is it blank line ? */
  1634.         if (last_line_blank) { /* Two consecutive blank lines. */
  1635.             more_data_fp = TRUE;
  1636.             break;
  1637.         }
  1638.         last_line_blank = TRUE;
  1639.  
  1640.         if (pt_in_iso_crv == 0) {
  1641.             if (xdatum == 0)
  1642.             continue;
  1643.             pt_in_iso_crv = xdatum;
  1644.         }
  1645.         if (xdatum > 0) {
  1646.             this_iso->p_count = xdatum;
  1647.             this_iso->next = this_plot->iso_crvs;
  1648.             this_plot->iso_crvs = this_iso;
  1649.             this_plot->num_iso_read++;
  1650.  
  1651.             if (xdatum != pt_in_iso_crv)
  1652.             this_plot->has_grid_topology = FALSE;
  1653.  
  1654.             this_iso = iso_alloc(pt_in_iso_crv);
  1655.             xdatum = 0;
  1656.             ydatum++;
  1657.         }
  1658.         continue;
  1659.         }
  1660.         last_line_blank = FALSE;
  1661.         if (xdatum >= this_iso->p_max) {
  1662.         /*
  1663.          * overflow about to occur. Extend size of points[] array. We
  1664.          * either double the size, or add 1000 points, whichever is a
  1665.          * smaller increment. Note i=p_max.
  1666.          */
  1667.         iso_extend(this_iso,
  1668.                xdatum + (xdatum < 1000 ? xdatum : 1000));
  1669.         }
  1670.         switch (num_col = sscanf(line, format, &x, &y, &z)) {
  1671.         case 3:        /* All parameter are specified. */
  1672.         if (!only_z || using_format) {
  1673.             switch (xyz_order) {
  1674.             case XYZ:    /* scanf(x,y,z) */
  1675.             this_iso->points[xdatum].x = x;
  1676.             this_iso->points[xdatum].y = y;
  1677.             this_iso->points[xdatum].z = z;
  1678.             break;
  1679.             case XZY:    /* scanf(x,z,y) */
  1680.             this_iso->points[xdatum].x = x;
  1681.             this_iso->points[xdatum].y = z;
  1682.             this_iso->points[xdatum].z = y;
  1683.             break;
  1684.             case YXZ:    /* scanf(y,x,z) */
  1685.             this_iso->points[xdatum].x = y;
  1686.             this_iso->points[xdatum].y = x;
  1687.             this_iso->points[xdatum].z = z;
  1688.             break;
  1689.             case ZXY:    /* scanf(z,x,y) */
  1690.             this_iso->points[xdatum].x = y;
  1691.             this_iso->points[xdatum].y = z;
  1692.             this_iso->points[xdatum].z = x;
  1693.             break;
  1694.             case YZX:    /* scanf(y,z,x) */
  1695.             this_iso->points[xdatum].x = z;
  1696.             this_iso->points[xdatum].y = x;
  1697.             this_iso->points[xdatum].z = y;
  1698.             break;
  1699.             case ZYX:    /* scanf(z,y,x) */
  1700.             this_iso->points[xdatum].x = z;
  1701.             this_iso->points[xdatum].y = y;
  1702.             this_iso->points[xdatum].z = x;
  1703.             break;
  1704.             }
  1705.             if (xyz_order != XYZ) {
  1706.             x = this_iso->points[xdatum].x;
  1707.             y = this_iso->points[xdatum].y;
  1708.             z = this_iso->points[xdatum].z;
  1709.             }
  1710.             if (!parametric)
  1711.             int_error("Must be in parametric mode.",
  1712.                   NO_CARET);
  1713.             break;
  1714.         }
  1715.         case 1:        /* only one number on the line */
  1716.         if (!only_z && !using_format)
  1717.             int_error("3 columns expected, only 1 found", c_token);
  1718.         /* assign that number to z */
  1719.         this_iso->points[xdatum].z = x;
  1720.         z = x;
  1721.         this_iso->points[xdatum].x = xdatum;
  1722.         x = this_iso->points[xdatum].x;
  1723.         this_iso->points[xdatum].y = ydatum;
  1724.         y = this_iso->points[xdatum].y;
  1725.         if (parametric)
  1726.             int_error("Must be in non parametric mode.",
  1727.                   NO_CARET);
  1728.         break;
  1729.         case 2:
  1730.         switch (xyz_order) {
  1731.         case YX:
  1732.             z = x;    /* Use z as temp */
  1733.             x = y;
  1734.             y = z;
  1735.             break;
  1736.         default:
  1737.             break;
  1738.         }
  1739.         switch (mapping3d) {
  1740.         case MAP3D_CARTESIAN:
  1741.             int_error("2 columns found, 1 or 3 expected",
  1742.                   c_token);
  1743.             break;
  1744.         case MAP3D_SPHERICAL:
  1745.             if (angles_format == ANGLES_DEGREES) {
  1746.             x *= DEG2RAD;    /* Convert to radians. */
  1747.             y *= DEG2RAD;
  1748.             }
  1749.             if( num_col == 2) z = 1.0;
  1750.             this_iso->points[xdatum].x = z*cos(x) * cos(y);
  1751.             this_iso->points[xdatum].y = z*sin(x) * cos(y);
  1752.             this_iso->points[xdatum].z = z*sin(y);
  1753.             break;
  1754.         case MAP3D_CYLINDRICAL:
  1755.             if (angles_format == ANGLES_DEGREES)
  1756.             x *= DEG2RAD;    /* Convert to radians. */
  1757.             if( num_col == 2) z = 1.0;
  1758.             this_iso->points[xdatum].x = z*cos(x);
  1759.             this_iso->points[xdatum].y = z*sin(x);
  1760.             this_iso->points[xdatum].z = y;
  1761.             break;
  1762.         }
  1763.         x = this_iso->points[xdatum].x;
  1764.         y = this_iso->points[xdatum].y;
  1765.         z = this_iso->points[xdatum].z;
  1766.         break;
  1767.         default:
  1768.         (void) sprintf(line, "bad data on line %d", l_num);
  1769.         int_error(line, c_token);
  1770.         }
  1771.  
  1772.         if (is_log_x) {
  1773.         if (x <= 0.0)
  1774.             int_error("X value must be above 0 for log scale!",
  1775.                   NO_CARET);
  1776.         else
  1777.             this_iso->points[xdatum].x =
  1778.             log(this_iso->points[xdatum].x)/log_base_log_x;
  1779.         }
  1780.         if (is_log_y) {
  1781.         if (y <= 0.0)
  1782.             int_error("Y value must be above 0 for log scale!",
  1783.                   NO_CARET);
  1784.         else
  1785.             this_iso->points[xdatum].y =
  1786.             log(this_iso->points[xdatum].y)/log_base_log_y;
  1787.         }
  1788.         if (is_log_z) {
  1789.         if (z <= 0.0)
  1790.             int_error("Z value must be above 0 for log scale!",
  1791.                   NO_CARET);
  1792.         else
  1793.             this_iso->points[xdatum].z =
  1794.             log(this_iso->points[xdatum].z)/log_base_log_z;
  1795.         }
  1796.         if (autoscale_lx) {
  1797.         if (x < xmin)
  1798.             xmin = x;
  1799.         if (x > xmax)
  1800.             xmax = x;
  1801.         }
  1802.         if (autoscale_ly) {
  1803.         if (y < ymin)
  1804.             ymin = y;
  1805.         if (y > ymax)
  1806.             ymax = y;
  1807.         }
  1808.         if (autoscale_lz) {
  1809.         if (z < zmin)
  1810.             zmin = z;
  1811.         if (z > zmax)
  1812.             zmax = z;
  1813.         }
  1814.         xdatum++;    
  1815.     }  /* end of while loop */
  1816.  
  1817.     if (xdatum > 0) {
  1818.         this_plot->num_iso_read++;    /* Update last iso. */
  1819.         this_iso->p_count = xdatum;
  1820.  
  1821.         this_iso->next = this_plot->iso_crvs;
  1822.         this_plot->iso_crvs = this_iso;
  1823.  
  1824.         if (xdatum != pt_in_iso_crv)
  1825.         this_plot->has_grid_topology = FALSE;
  1826.  
  1827.     } else {
  1828.         iso_free(this_iso);    /* Free last allocation. */
  1829.     }
  1830.     }                /* MOD-RKC else of binary */
  1831.  
  1832.     if (index >= 0) more_data_fp = FALSE; /* Only one data set please. */
  1833.  
  1834.     if (!more_data_fp) {
  1835.       if (this_plot->num_iso_read <= 1)
  1836.       this_plot->has_grid_topology = FALSE;
  1837. #if defined(unix) || defined(PIPES)
  1838.       if (pipe_open) {
  1839.     if (this_plot->has_grid_topology && !hidden3d) {
  1840.       (void) pclose(data_fp);
  1841.       pipe_open = FALSE;
  1842.     }
  1843.       } else 
  1844. #endif /* unix || PIPES */
  1845.       {
  1846.     (void) fclose(data_fp);  
  1847.     data_fp = NULL;
  1848.       }
  1849.     }
  1850.  
  1851.     if (dgrid3d) grid_nongrid_data(this_plot);
  1852.  
  1853.     if (this_plot->num_iso_read <= 1)
  1854.     this_plot->has_grid_topology = FALSE;
  1855.     if (this_plot->has_grid_topology && !hidden3d) {
  1856.     struct iso_curve *new_icrvs = NULL;
  1857.     int             num_new_iso = this_plot->iso_crvs->p_count, len_new_iso = this_plot->num_iso_read;
  1858.  
  1859.     /* Now we need to set the other direction (pseudo) isolines. */
  1860.     for (i = 0; i < num_new_iso; i++) {
  1861.         struct iso_curve *new_icrv = iso_alloc(len_new_iso);
  1862.  
  1863.         new_icrv->p_count = len_new_iso;
  1864.  
  1865.         for (j = 0, this_iso = this_plot->iso_crvs;
  1866.          this_iso != NULL;
  1867.          j++, this_iso = this_iso->next) {
  1868.         new_icrv->points[j].x = this_iso->points[i].x;
  1869.         new_icrv->points[j].y = this_iso->points[i].y;
  1870.         new_icrv->points[j].z = this_iso->points[i].z;
  1871.         }
  1872.  
  1873.         new_icrv->next = new_icrvs;
  1874.         new_icrvs = new_icrv;
  1875.     }
  1876.  
  1877.     /* Append the new iso curves after the read ones. */
  1878.     for (this_iso = this_plot->iso_crvs;
  1879.          this_iso->next != NULL;
  1880.          this_iso = this_iso->next);
  1881.         this_iso->next = new_icrvs;
  1882.     }
  1883. }
  1884.  
  1885. /*
  1886.  * print_points: a debugging routine to print out the points of a curve, and
  1887.  * the curve structure. If curve<0, then we print the list of curves.
  1888.  */
  1889.  
  1890. static char    *plot_type_names[4] =
  1891. {
  1892.     "Function", "Data", "3D Function", "3d data"
  1893. };
  1894. static char    *plot_style_names[6] =
  1895. {
  1896.     "Lines", "Points", "Impulses", "LinesPoints", "Dots", "Errorbars"
  1897. };
  1898.  
  1899. print_points(curve)
  1900.     int             curve;    /* which curve to print */
  1901. {
  1902.     register struct curve_points *this_plot;
  1903.     int             i;
  1904.  
  1905.     if (curve < 0) {
  1906.     for (this_plot = first_plot, i = 0;
  1907.          this_plot != NULL;
  1908.          i++, this_plot = this_plot->next_cp) {
  1909.         printf("Curve %d:\n", i);
  1910.         if ((int) this_plot->plot_type >= 0 && (int) (this_plot->plot_type) < 4)
  1911.         printf("Plot type %d: %s\n", (int) (this_plot->plot_type),
  1912.                plot_type_names[(int) (this_plot->plot_type)]);
  1913.         else
  1914.         printf("Plot type %d: BAD\n", (int) (this_plot->plot_type));
  1915.         if ((int) this_plot->plot_style >= 0 && (int) (this_plot->plot_style) < 6)
  1916.         printf("Plot style %d: %s\n", (int) (this_plot->plot_style),
  1917.                plot_style_names[(int) (this_plot->plot_style)]);
  1918.         else
  1919.         printf("Plot style %d: BAD\n", (int) (this_plot->plot_style));
  1920.         printf("Plot title: '%s'\n", this_plot->title);
  1921.         printf("Line type %d\n", this_plot->line_type);
  1922.         printf("Point type %d\n", this_plot->point_type);
  1923.         printf("max points %d\n", this_plot->p_max);
  1924.         printf("current points %d\n", this_plot->p_count);
  1925.         printf("\n");
  1926.     }
  1927.     } else {
  1928.     for (this_plot = first_plot, i = 0;
  1929.          i < curve && this_plot != NULL;
  1930.          i++, this_plot = this_plot->next_cp);
  1931.     if (this_plot == NULL)
  1932.         printf("Curve %d does not exist; list has %d curves\n", curve, i);
  1933.     else {
  1934.         printf("Curve %d, %d points\n", curve, this_plot->p_count);
  1935.         for (i = 0; i < this_plot->p_count; i++) {
  1936.         printf("%c x=%g y=%g z=%g ylow=%g yhigh=%g\n",
  1937.                this_plot->points[i].type == INRANGE ? 'i'
  1938.                : this_plot->points[i].type == OUTRANGE ? 'o'
  1939.                : 'u',
  1940.                this_plot->points[i].x,
  1941.                this_plot->points[i].y,
  1942.                this_plot->points[i].z,
  1943.                this_plot->points[i].ylow,
  1944.                this_plot->points[i].yhigh);
  1945.         }
  1946.         printf("\n");
  1947.     }
  1948.     }
  1949. }
  1950.  
  1951. print_table()
  1952. {
  1953.     register struct curve_points *this_plot;
  1954.     int             i, curve;
  1955.  
  1956.     for (this_plot = first_plot, curve = 0; this_plot != NULL;
  1957.      curve++, this_plot = this_plot->next_cp) {
  1958.     fprintf(outfile, "Curve %d, %d points\n", curve, this_plot->p_count);
  1959.     for (i = 0; i < this_plot->p_count; i++) {
  1960.         fprintf(outfile, "%c x=%g y=%g\n",
  1961.             this_plot->points[i].type == INRANGE ? 'i'
  1962.             : this_plot->points[i].type == OUTRANGE ? 'o'
  1963.             : 'u',
  1964.             this_plot->points[i].x,
  1965.             this_plot->points[i].y);
  1966.     }
  1967.     fprintf(outfile, "\n");
  1968.     }
  1969.     fflush(outfile);
  1970. }
  1971.  
  1972. print_3dtable()
  1973. {
  1974.     register struct surface_points *this_3dplot;
  1975.     int             i, curve;
  1976.     struct iso_curve *isocrv;
  1977.  
  1978.     for (this_3dplot = first_3dplot, curve = 0; this_3dplot != NULL;
  1979.      curve++, this_3dplot = this_3dplot->next_sp) {
  1980.     isocrv = this_3dplot->iso_crvs;
  1981.     fprintf(outfile, "Curve %d, %d points\n", curve, isocrv->p_count);
  1982.     for (i = 0; i < isocrv->p_count; i++) {
  1983.         fprintf(outfile, "%c x=%g y=%g z=%g\n",
  1984.             isocrv->points[i].type == INRANGE ? 'i'
  1985.             : isocrv->points[i].type == OUTRANGE ? 'o'
  1986.             : 'u',
  1987.             isocrv->points[i].x,
  1988.             isocrv->points[i].y,
  1989.             isocrv->points[i].z);
  1990.     }
  1991.     fprintf(outfile, "\n");
  1992.     }
  1993.     fflush(outfile);
  1994. }
  1995.  
  1996. /*
  1997.  * This parses the plot command after any range specifications. To support
  1998.  * autoscaling on the x axis, we want any data files to define the x range,
  1999.  * then to plot any functions using that range. We thus parse the input
  2000.  * twice, once to pick up the data files, and again to pick up the functions.
  2001.  * Definitions are processed twice, but that won't hurt.
  2002.  */
  2003. eval_plots()
  2004. {
  2005.     register int    i;
  2006.     register struct curve_points *this_plot, **tp_ptr;
  2007.     register int    start_token, end_token;
  2008.     register int    begin_token;
  2009.     double          x_min, x_max, y_min, y_max;
  2010.     register double x, xdiff, temp;
  2011.     static struct value a;
  2012.     TBOOLEAN         ltmp, some_data_files = FALSE,is_log_func = FALSE;
  2013.     int             plot_num, line_num, point_num, xparam = 0;
  2014.     char           *xtitle;
  2015.     void            parametric_fixup();
  2016.  
  2017.     if (autoscale_ly) {
  2018.     ymin = VERYLARGE;
  2019.     ymax = -VERYLARGE;
  2020.     } else if (is_log_y && (ymin <= 0.0 || ymax <= 0.0))
  2021.     int_error("y range must be above 0 for log scale!",
  2022.           NO_CARET);
  2023.  
  2024.     tp_ptr = &(first_plot);
  2025.     plot_num = 0;
  2026.     line_num = 0;        /* default line type */
  2027.     point_num = 0;        /* default point type */
  2028.  
  2029.     xtitle = NULL;
  2030.  
  2031.     begin_token = c_token;
  2032.  
  2033.     /*** First Pass: Read through data files ***
  2034.      * This pass serves to set the xrange and to parse the command, as well
  2035.      * as filling in every thing except the function data. That is done after
  2036.      * the xrange is defined.
  2037.      */
  2038.     while (TRUE) {
  2039.     if (END_OF_COMMAND)
  2040.         int_error("function to plot expected", c_token);
  2041.  
  2042.     start_token = c_token;
  2043.  
  2044.     if (is_definition(c_token)) {
  2045.         define();
  2046.     } else {
  2047.         plot_num++;
  2048.  
  2049.         if (isstring(c_token)) {    /* data file to plot */
  2050.         if (parametric && xparam)
  2051.             int_error("previous parametric function not fully specified",
  2052.                   c_token);
  2053.  
  2054.         if (!some_data_files && autoscale_lx) {
  2055.             xmin = VERYLARGE;
  2056.             xmax = -VERYLARGE;
  2057.         }
  2058.         some_data_files = TRUE;
  2059.  
  2060.         if (*tp_ptr)
  2061.             this_plot = *tp_ptr;
  2062.         else {        /* no memory malloc()'d there yet */
  2063.             this_plot = cp_alloc(MIN_CRV_POINTS);
  2064.             *tp_ptr = this_plot;
  2065.         }
  2066.         this_plot->plot_type = DATA;
  2067.         this_plot->plot_style = data_style;
  2068.         end_token = c_token;
  2069.         get_data(this_plot);    /* this also parses the using option */
  2070.         } else {        /* function to plot */
  2071.         if (parametric)    /* working on x parametric function */
  2072.             xparam = 1 - xparam;
  2073.         if (*tp_ptr) {
  2074.             this_plot = *tp_ptr;
  2075.             cp_extend(this_plot, samples + 1);
  2076.         } else {    /* no memory malloc()'d there yet */
  2077.             this_plot = cp_alloc(samples + 1);
  2078.             *tp_ptr = this_plot;
  2079.         }
  2080.         this_plot->plot_type = FUNC;
  2081.         this_plot->plot_style = func_style;
  2082.         dummy_func = &plot_func;
  2083.         plot_func.at = temp_at();
  2084.         /* ignore it for now */
  2085.         end_token = c_token - 1;
  2086.         }
  2087.  
  2088.         if (almost_equals(c_token, "t$itle")) {
  2089.         if (parametric) {
  2090.             if (xparam)
  2091.             int_error(
  2092.                      "\"title\" allowed only after parametric function fully specified",
  2093.                      c_token);
  2094.             else if (xtitle != NULL)
  2095.             xtitle[0] = '\0';    /* Remove default title . */
  2096.         }
  2097.         c_token++;
  2098.         if (isstring(c_token)) {
  2099.             m_quote_capture(&(this_plot->title), c_token, c_token);
  2100.         } else {
  2101.             int_error("expecting \"title\" for plot", c_token);
  2102.         }
  2103.         c_token++;
  2104.         } else if (almost_equals(c_token, "not$itle")) {
  2105.             c_token++;
  2106.         } else {
  2107.         m_capture(&(this_plot->title), start_token, end_token);
  2108.         if (xparam)
  2109.             xtitle = this_plot->title;
  2110.         }
  2111.  
  2112.         this_plot->line_type = line_num;
  2113.         this_plot->point_type = point_num;
  2114.  
  2115.         if (almost_equals(c_token, "w$ith")) {
  2116.         if (parametric && xparam)
  2117.             int_error("\"with\" allowed only after parametric function fully specified",
  2118.                   c_token);
  2119.         this_plot->plot_style = get_style();
  2120.         }
  2121.         if (!equals(c_token, ",") && !END_OF_COMMAND) {
  2122.         struct value    t;
  2123.         this_plot->line_type = (int) real(const_express(&t)) - 1;
  2124.         }
  2125.         if (!equals(c_token, ",") && !END_OF_COMMAND) {
  2126.         struct value    t;
  2127.         this_plot->point_type = (int) real(const_express(&t)) - 1;
  2128.         }
  2129.         if ((this_plot->plot_style == POINTSTYLE) ||
  2130.         (this_plot->plot_style == LINESPOINTS) ||
  2131.         (this_plot->plot_style == ERRORBARS))
  2132.         if (!xparam)
  2133.             point_num++;
  2134.         if (!xparam)
  2135.         line_num++;
  2136.  
  2137.         if (this_plot->plot_type == DATA)
  2138.         /* now that we know the plot style, adjust the yrange */
  2139.         adjust_yrange(this_plot);
  2140.  
  2141.         tp_ptr = &(this_plot->next_cp);
  2142.     }
  2143.  
  2144.     if (equals(c_token, ","))
  2145.         c_token++;
  2146.     else
  2147.         break;
  2148.     }
  2149.  
  2150.     if (parametric && xparam)
  2151.     int_error("parametric function not fully specified", NO_CARET);
  2152.  
  2153.     if (parametric) {
  2154.     /* Swap t and x ranges for duration of these eval_plot computations. */
  2155.     ltmp = autoscale_lx;
  2156.     autoscale_lx = autoscale_lt;
  2157.     autoscale_lt = ltmp;
  2158.     temp = xmin;
  2159.     xmin = tmin;
  2160.     tmin = temp;
  2161.     temp = xmax;
  2162.     xmax = tmax;
  2163.     tmax = temp;
  2164.     }
  2165.     /*** Second Pass: Evaluate the functions ***/
  2166.     /*
  2167.      * Everything is defined now, except the function data. We expect no
  2168.      * syntax errors, etc, since the above parsed it all. This makes the code
  2169.      * below simpler. If autoscale_ly, the yrange may still change.
  2170.      */
  2171.     if (fabs(xmax - xmin) < zero)
  2172.     if (autoscale_lx) {
  2173.         fprintf(stderr, "Warning: empty %c range [%g:%g], ",
  2174.             parametric ? 't' : 'x', xmin, xmax);
  2175.         if (fabs(xmin) < zero) {
  2176.         /* completely arbitary */
  2177.         xmin = -1.;
  2178.         xmax = 1.;
  2179.         } else {
  2180.         /* expand range by 10% in either direction */
  2181.         xmin = xmin * 0.9;
  2182.         xmax = xmax * 1.1;
  2183.         }
  2184.         fprintf(stderr, "adjusting to [%g:%g]\n", xmin, xmax);
  2185.     } else {
  2186.         int_error("x range is less than `zero`", c_token);
  2187.     }
  2188.  
  2189.     /* give error if xrange badly set from missing datafile error */
  2190.     if (xmin == VERYLARGE || xmax == -VERYLARGE) {
  2191.     int_error("x range is invalid", c_token);
  2192.     }
  2193.     if (is_log_x) {
  2194.     if (xmin <= 0.0 || xmax <= 0.0)
  2195.         int_error("x range must be greater than 0 for log scale!", NO_CARET);
  2196.     x_min = log(xmin)/log_base_log_x;
  2197.     x_max = log(xmax)/log_base_log_x;
  2198.     } else {
  2199.     x_min = xmin;
  2200.     x_max = xmax;
  2201.     }
  2202.  
  2203.     xdiff = (x_max - x_min) / (samples - 1);
  2204.  
  2205.     tp_ptr = &(first_plot);
  2206.     plot_num = 0;
  2207.     this_plot = first_plot;
  2208.     c_token = begin_token;    /* start over */
  2209.  
  2210.     /* Read through functions */
  2211.     while (TRUE) {
  2212.     if (is_definition(c_token)) {
  2213.         define();
  2214.     } else {
  2215.         plot_num++;
  2216.         if (isstring(c_token)) {    /* data file to plot */
  2217.         /* ignore this now */
  2218.         c_token++;
  2219.  
  2220.         /*
  2221.          * jev -- support for passing data from file thru user
  2222.          * function
  2223.          */
  2224.         if (almost_equals(c_token, "thru$")) {
  2225.             struct udft_entry tmp;
  2226.             c_token++;
  2227.             dummy_func = &tmp;
  2228.             (void) temp_at();
  2229.         }
  2230.         if (almost_equals(c_token, "u$sing")) {
  2231.             c_token++;    /* skip "using" */
  2232.             if (!isstring(c_token)) {
  2233.             struct value    a;
  2234.             (void) magnitude(const_express(&a));    /* skip xcol */
  2235.             if (equals(c_token, ":")) {
  2236.                 c_token++;    /* skip ":" */
  2237.                 (void) magnitude(const_express(&a));    /* skip ycol */
  2238.             }
  2239.             if (equals(c_token, ":")) {
  2240.                 c_token++;    /* skip ":" */
  2241.                 (void) magnitude(const_express(&a));    /* skip yemin */
  2242.             }
  2243.             if (equals(c_token, ":")) {
  2244.                 c_token++;    /* skip ":" */
  2245.                 (void) magnitude(const_express(&a));    /* skip yemax */
  2246.             }
  2247.             if (equals(c_token, ":")) {
  2248.                 c_token++;    /* skip ":" */
  2249.                 (void) magnitude(const_express(&a));    /* skip wcol */
  2250.             }
  2251.             }
  2252.             if (isstring(c_token))
  2253.             c_token++;    /* skip format string */
  2254.         }
  2255.         } else {        /* function to plot */
  2256.         if (parametric)    /* working on x parametric function */
  2257.             xparam = 1 - xparam;
  2258.         dummy_func = &plot_func;
  2259.         plot_func.at = temp_at();    /* reparse function */
  2260.  
  2261.         is_log_func=parametric?(xparam?is_log_x:is_log_y):is_log_y;
  2262.         for (i = 0; i < samples; i++) {
  2263.             x = x_min + i * xdiff;
  2264.             /* if (is_log_x) PEM fix logscale x axis */
  2265.             /* x = pow(base_log_x,x); 26-Sep-89 */
  2266.             (void) Gcomplex(&plot_func.dummy_values[0],
  2267.                    is_log_x ? pow(base_log_x, x) : x,
  2268.                    0.0);
  2269.  
  2270.             evaluate_at(plot_func.at, &a);
  2271.  
  2272.             if (undefined || (fabs(imag(&a)) > zero)) {
  2273.             this_plot->points[i].type = UNDEFINED;
  2274.             continue;
  2275.             }
  2276.             temp = real(&a);
  2277.             
  2278.             if (is_log_func && temp < 0.0) {
  2279.             this_plot->points[i].type = UNDEFINED;
  2280.             continue;
  2281.             }
  2282.             this_plot->points[i].x = x;
  2283.             this_plot->points[i].z = -1.0;  /* width of box not specified */
  2284.  
  2285.             if (is_log_func) {
  2286.             if (temp == 0.0) {
  2287.                 this_plot->points[i].type = OUTRANGE;
  2288.                 this_plot->points[i].y = -VERYLARGE;
  2289.                 continue;
  2290.             } else {
  2291.                 this_plot->points[i].y = log(temp)/log_base_log_y;
  2292.             }
  2293.             } else
  2294.             this_plot->points[i].y = temp;
  2295.  
  2296.             if (autoscale_ly || polar
  2297.             || inrange(temp, ymin, ymax)) {
  2298.             this_plot->points[i].type = INRANGE;
  2299.             /* When xparam is 1 we are not really computing y's! */
  2300.             if (!xparam && autoscale_ly) {
  2301.                 if (temp < ymin)
  2302.                 ymin = temp;
  2303.                 if (temp > ymax)
  2304.                 ymax = temp;
  2305.             }
  2306.             } else
  2307.             this_plot->points[i].type = OUTRANGE;
  2308.         }
  2309.         this_plot->p_count = i;    /* samples */
  2310.         }
  2311.  
  2312.         /* title was handled above */
  2313.         if (almost_equals(c_token, "t$itle")) {
  2314.         c_token++;
  2315.         c_token++;
  2316.         } else if (almost_equals(c_token, "not$itle")) {
  2317.             c_token++;
  2318.         }
  2319.         /* style was handled above */
  2320.         if (almost_equals(c_token, "w$ith")) {
  2321.         c_token++;
  2322.         c_token++;
  2323.         }
  2324.         /* line and point types were handled above */
  2325.         if (!equals(c_token, ",") && !END_OF_COMMAND) {
  2326.         struct value    t;
  2327.         (void) real(const_express(&t));
  2328.         }
  2329.         if (!equals(c_token, ",") && !END_OF_COMMAND) {
  2330.         struct value    t;
  2331.         (void) real(const_express(&t));
  2332.         }
  2333.         tp_ptr = &(this_plot->next_cp);    /* used below */
  2334.         this_plot = this_plot->next_cp;
  2335.     }
  2336.  
  2337.     if (equals(c_token, ","))
  2338.         c_token++;
  2339.     else
  2340.         break;
  2341.     }
  2342.  
  2343.     /* throw out all curve_points at end of list, that we don't need  */
  2344.     cp_free(*tp_ptr);
  2345.     *tp_ptr = NULL;
  2346.  
  2347.     if (fabs(ymax - ymin) < zero)
  2348.     /* if autoscale, widen range */
  2349.     if (autoscale_ly) {
  2350.         fprintf(stderr, "Warning: empty y range [%g:%g], ", ymin, ymax);
  2351.         if (fabs(ymin) < zero) {
  2352.         ymin = -1.;
  2353.         ymax = 1.;
  2354.         } else {
  2355.         /* expand range by 10% in either direction */
  2356.         ymin = ymin * 0.9;
  2357.         ymax = ymax * 1.1;
  2358.         }
  2359.         fprintf(stderr, "adjusting to [%g:%g]\n", ymin, ymax);
  2360.     } else {
  2361.         int_error("y range is less than `zero`", c_token);
  2362.     }
  2363.  
  2364.     /* Now we finally know the real ymin and ymax */
  2365.     if (is_log_y) {
  2366.     y_min = log(ymin)/log_base_log_y;
  2367.     y_max = log(ymax)/log_base_log_y;
  2368.     } else {
  2369.     y_min = ymin;
  2370.     y_max = ymax;
  2371.     }
  2372.     capture(replot_line, plot_token, c_token);
  2373.  
  2374.     if (parametric) {
  2375.     /* Now put t and x ranges back before we actually plot anything. */
  2376.     ltmp = autoscale_lx;
  2377.     autoscale_lx = autoscale_lt;
  2378.     autoscale_lt = ltmp;
  2379.     temp = xmin;
  2380.     xmin = tmin;
  2381.     tmin = temp;
  2382.     temp = xmax;
  2383.     xmax = tmax;
  2384.     tmax = temp;
  2385.     if (some_data_files && autoscale_lx) {
  2386.         /*
  2387.          * Stop any further autoscaling in this case (may be a mistake,
  2388.          * have to consider what is really wanted some day in the
  2389.          * future--jdc).
  2390.          */
  2391.         autoscale_lx = 0;
  2392.     }
  2393.     /* Now actually fix the plot pairs to be single plots. */
  2394.     parametric_fixup(first_plot, &plot_num, &x_min, &x_max);
  2395.     }
  2396.     if (strcmp(term_tbl[term].name, "table") == 0)
  2397.     print_table();
  2398.     else
  2399.     do_plot(first_plot, plot_num, x_min, x_max, y_min, y_max);
  2400.     cp_free(first_plot);
  2401.     first_plot = NULL;
  2402. }
  2403.  
  2404. static void 
  2405. parse_title(crnt_param, start_token, end_token,
  2406.         xtitle, ytitle, this_plot, do_parse)
  2407. int crnt_param, start_token, end_token;
  2408. char **xtitle, **ytitle;
  2409. struct surface_points *this_plot;
  2410. TBOOLEAN do_parse;
  2411. {
  2412.     static char title[256];
  2413.  
  2414.     if (do_parse) {
  2415.     if (almost_equals(c_token, "t$itle")) {
  2416.         if (parametric) {
  2417.         if (crnt_param)
  2418.             int_error("\"title\" allowed only after parametric function fully specified",
  2419.                   c_token);
  2420.         else {
  2421.             /* Remove default title */
  2422.             if (*xtitle != NULL)
  2423.             (*xtitle)[0] = '\0';
  2424.             if (*ytitle != NULL)
  2425.             (*ytitle)[0] = '\0';
  2426.         }
  2427.         }
  2428.         c_token++;
  2429.         if (isstring(c_token)) {
  2430.         m_quote_capture(&(this_plot->title), c_token, c_token);
  2431.         } else {
  2432.         int_error("expecting \"title\" for plot", c_token);
  2433.         }
  2434.         c_token++;
  2435.     }  else if (almost_equals(c_token, "not$itle")) {
  2436.         c_token++;
  2437.     }  else {
  2438.         m_capture(&(this_plot->title), start_token, end_token);
  2439.         if (crnt_param == 1)
  2440.         *xtitle = this_plot->title;
  2441.         if (crnt_param == 2)
  2442.         *ytitle = this_plot->title;
  2443.     }
  2444.     strncpy(title, this_plot->title, 255);
  2445.     }
  2446.     else {
  2447.     this_plot->title = alloc(strlen(title) + 1);
  2448.     strcpy(this_plot->title, title);
  2449.     }
  2450. }
  2451.  
  2452. /*
  2453.  * This parses the splot command after any range specifications. To support
  2454.  * autoscaling on the x/z axis, we want any data files to define the x/y
  2455.  * range, then to plot any functions using that range. We thus parse the
  2456.  * input twice, once to pick up the data files, and again to pick up the
  2457.  * functions. Definitions are processed twice, but that won't hurt.
  2458.  */
  2459. eval_3dplots()
  2460. {
  2461.     register int    i, j;
  2462.     register struct surface_points *this_plot, **tp_3d_ptr;
  2463.     register int    start_token, end_token;
  2464.     register int    begin_token;
  2465.     double          x_min, x_max, y_min, y_max, z_min, z_max;
  2466.     register double x, xdiff, xisodiff, y, ydiff, yisodiff, temp;
  2467.     static struct value a;
  2468.     TBOOLEAN         ltmp, some_data_files = FALSE,is_log_func = FALSE;
  2469.     int             plot_num, line_num, point_num, crnt_param = 0;    /* 0=z, 1=x, 2=y */
  2470.     char           *xtitle;
  2471.     char           *ytitle;
  2472.     void            parametric_3dfixup();
  2473.  
  2474.     if (autoscale_lz) {
  2475.     zmin = VERYLARGE;
  2476.     zmax = -VERYLARGE;
  2477.     } else if (is_log_z && (zmin <= 0.0 || zmax <= 0.0))
  2478.     int_error("z range must be above 0 for log scale!",
  2479.           NO_CARET);
  2480.  
  2481.     tp_3d_ptr = &(first_3dplot);
  2482.     plot_num = 0;
  2483.     line_num = 0;        /* default line type */
  2484.     point_num = 0;        /* default point type */
  2485.  
  2486.     xtitle = NULL;
  2487.     ytitle = NULL;
  2488.  
  2489.     begin_token = c_token;
  2490.  
  2491.     /*** First Pass: Read through data files ***/
  2492.     /*
  2493.      * This pass serves to set the x/yranges and to parse the command, as
  2494.      * well as filling in every thing except the function data. That is done
  2495.      * after the x/yrange is defined.
  2496.      */
  2497.     while (TRUE) {
  2498.     if (END_OF_COMMAND)
  2499.         int_error("function to plt3d expected", c_token);
  2500.  
  2501.     start_token = c_token;
  2502.  
  2503.     if (is_definition(c_token)) {
  2504.         define();
  2505.     } else {
  2506.         plot_num++;
  2507.  
  2508.         if (isstring(c_token)) {    /* data file to plot */
  2509.         int line_type = line_num,
  2510.             point_type = point_num,
  2511.             plot_style = data_style,
  2512.             first_mesh = TRUE;
  2513.  
  2514.         if (parametric && crnt_param != 0)
  2515.             int_error("previous parametric function not fully specified",
  2516.                   c_token);
  2517.  
  2518.         if (!some_data_files) {
  2519.             if (autoscale_lx) {
  2520.             xmin = VERYLARGE;
  2521.             xmax = -VERYLARGE;
  2522.             }
  2523.             if (autoscale_ly) {
  2524.             ymin = VERYLARGE;
  2525.             ymax = -VERYLARGE;
  2526.             }
  2527.         }
  2528.         some_data_files = TRUE;
  2529.  
  2530.         do {
  2531.             if (*tp_3d_ptr)
  2532.             this_plot = *tp_3d_ptr;
  2533.             else {        /* no memory malloc()'d there yet */
  2534.             /* Allocate enough isosamples and samples */
  2535.             this_plot = sp_alloc(0, 0, 0, 0);
  2536.             *tp_3d_ptr = this_plot;
  2537.             }
  2538.  
  2539.             this_plot->plot_type = DATA3D;
  2540.             end_token = c_token;
  2541.             /* this also parses index/using option */
  2542.             get_3ddata(this_plot);
  2543.  
  2544.             parse_title(crnt_param, start_token, end_token,
  2545.                 &xtitle, &ytitle, this_plot, first_mesh);
  2546.             if (!first_mesh) plot_num++;
  2547.  
  2548.             if (first_mesh) {
  2549.             if (almost_equals(c_token, "w$ith")) {
  2550.                 plot_style = this_plot->plot_style = get_style();
  2551.             }
  2552.             if (!equals(c_token, ",") && !END_OF_COMMAND) {
  2553.                 struct value    t;
  2554.                 line_type = (int) real(const_express(&t)) - 1;
  2555.             }
  2556.             if (!equals(c_token, ",") && !END_OF_COMMAND) {
  2557.                 struct value    t;
  2558.                 point_type = (int) real(const_express(&t)) - 1;
  2559.             }
  2560.             first_mesh = FALSE;
  2561.             }
  2562.  
  2563.             this_plot->line_type = line_type;
  2564.             this_plot->point_type = point_type;
  2565.             this_plot->plot_style = plot_style;
  2566.  
  2567.             tp_3d_ptr = &(this_plot->next_sp);
  2568.         }
  2569.         while (more_data_fp);
  2570.         } else {        /* function to plot */
  2571.         if (parametric)    /* Rotate between x/y/z axes */
  2572.             crnt_param = (crnt_param + 1) % 3;
  2573.         if (*tp_3d_ptr) {
  2574.             this_plot = *tp_3d_ptr;
  2575.             if (!hidden3d)
  2576.             sp_replace(this_plot, samples_1, iso_samples_1,
  2577.                                    samples_2, iso_samples_2);
  2578.             else
  2579.             sp_replace(this_plot, iso_samples_1, 0,
  2580.                                    0, iso_samples_2);
  2581.         } else {    /* no memory malloc()'d there yet */
  2582.             /* Allocate enough isosamples and samples */
  2583.             if (!hidden3d)
  2584.             this_plot = sp_alloc(samples_1, iso_samples_1,
  2585.                                              samples_2, iso_samples_2);
  2586.             else
  2587.             this_plot = sp_alloc(iso_samples_1, 0,
  2588.                                              0, iso_samples_2);
  2589.             *tp_3d_ptr = this_plot;
  2590.         }
  2591.  
  2592.         this_plot->plot_type = FUNC3D;
  2593.         this_plot->has_grid_topology = TRUE;
  2594.         this_plot->plot_style = func_style;
  2595.         dummy_func = &plot_func;
  2596.         plot_func.at = temp_at();
  2597.         /* ignore it for now */
  2598.         end_token = c_token - 1;
  2599.  
  2600.         parse_title(crnt_param, start_token, end_token,
  2601.                 &xtitle, &ytitle, this_plot, TRUE);
  2602.  
  2603.         this_plot->line_type = line_num;
  2604.         this_plot->point_type = point_num;
  2605.  
  2606.         if (almost_equals(c_token, "w$ith")) {
  2607.             this_plot->plot_style = get_style();
  2608.         }
  2609.         if (!equals(c_token, ",") && !END_OF_COMMAND) {
  2610.             struct value    t;
  2611.             this_plot->line_type = (int) real(const_express(&t)) - 1;
  2612.         }
  2613.         if (!equals(c_token, ",") && !END_OF_COMMAND) {
  2614.             struct value    t;
  2615.             this_plot->point_type = (int) real(const_express(&t)) - 1;
  2616.         }
  2617.  
  2618.         tp_3d_ptr = &(this_plot->next_sp);
  2619.         }
  2620.  
  2621.         if ((this_plot->plot_style == POINTSTYLE) ||
  2622.         (this_plot->plot_style == LINESPOINTS) ||
  2623.         (this_plot->plot_style == ERRORBARS))
  2624.         if (crnt_param == 0)
  2625.             point_num +=
  2626.             1 + (draw_contour != 0)
  2627.             + (hidden3d != 0);
  2628.         if (crnt_param == 0)
  2629.         line_num += 1 + (draw_contour != 0)
  2630.             + (hidden3d != 0);
  2631.     }
  2632.  
  2633.     if (equals(c_token, ","))
  2634.         c_token++;
  2635.     else
  2636.         break;
  2637.     }
  2638.  
  2639.     if (parametric && crnt_param != 0)
  2640.     int_error("parametric function not fully specified", NO_CARET);
  2641.  
  2642.     if (parametric) {
  2643.     /*
  2644.      * Swap u/v and x/y ranges for duration of these eval_plot
  2645.      * computations.
  2646.      */
  2647.     ltmp = autoscale_lx;
  2648.     autoscale_lx = autoscale_lu;
  2649.     autoscale_lu = ltmp;
  2650.     ltmp = autoscale_ly;
  2651.     autoscale_ly = autoscale_lv;
  2652.     autoscale_lv = ltmp;
  2653.     temp = xmin;
  2654.     xmin = umin;
  2655.     umin = temp;
  2656.     temp = xmax;
  2657.     xmax = umax;
  2658.     umax = temp;
  2659.     temp = ymin;
  2660.     ymin = vmin;
  2661.     vmin = temp;
  2662.     temp = ymax;
  2663.     ymax = vmax;
  2664.     vmax = temp;
  2665.     }
  2666.     /*** Second Pass: Evaluate the functions ***/
  2667.     /*
  2668.      * Everything is defined now, except the function data. We expect no
  2669.      * syntax errors, etc, since the above parsed it all. This makes the code
  2670.      * below simpler. If autoscale_ly, the yrange may still change.
  2671.      */
  2672.     if (xmin == xmax)
  2673.     if (autoscale_lx) {
  2674.         fprintf(stderr, "Warning: empty x range [%g:%g], ",
  2675.             xmin, xmax);
  2676.         if (xmin == 0.0) {
  2677.         /* completely arbitary */
  2678.         xmin = -1.;
  2679.         xmax = 1.;
  2680.         } else {
  2681.         /* expand range by 10% in either direction */
  2682.         xmin = xmin * 0.9;
  2683.         xmax = xmax * 1.1;
  2684.         }
  2685.         fprintf(stderr, "adjusting to [%g:%g]\n", xmin, xmax);
  2686.     } else {
  2687.         int_error("x range is empty", c_token);
  2688.     }
  2689.  
  2690.     if (ymin == ymax)
  2691.     if (autoscale_ly) {
  2692.         fprintf(stderr, "Warning: empty y range [%g:%g], ",
  2693.             ymin, ymax);
  2694.         if (ymin == 0.0) {
  2695.         /* completely arbitary */
  2696.         ymin = -1.;
  2697.         ymax = 1.;
  2698.         } else {
  2699.         /* expand range by 10% in either direction */
  2700.         ymin = ymin * 0.9;
  2701.         ymax = ymax * 1.1;
  2702.         }
  2703.         fprintf(stderr, "adjusting to [%g:%g]\n", ymin, ymax);
  2704.     } else {
  2705.         int_error("y range is empty", c_token);
  2706.     }
  2707.  
  2708.     /* give error if xrange badly set from missing datafile error */
  2709.     if (xmin == VERYLARGE || xmax == -VERYLARGE) {
  2710.     int_error("x range is invalid", c_token);
  2711.     }
  2712.     if (is_log_x) {
  2713.     if (xmin <= 0.0 || xmax <= 0.0)
  2714.         int_error("x range must be greater than 0 for log scale!", NO_CARET);
  2715.     x_min = log(xmin)/log_base_log_x;
  2716.     x_max = log(xmax)/log_base_log_x;
  2717.     } else {
  2718.     x_min = xmin;
  2719.     x_max = xmax;
  2720.     }
  2721.  
  2722.     if (is_log_y) {
  2723.     if (ymin <= 0.0 || ymax <= 0.0)
  2724.         int_error("y range must be greater than 0 for log scale!", NO_CARET);
  2725.     y_min = log(ymin)/log_base_log_y;
  2726.     y_max = log(ymax)/log_base_log_y;
  2727.     } else {
  2728.     y_min = ymin;
  2729.     y_max = ymax;
  2730.     }
  2731.  
  2732.     if (samples_1 < 2 || samples_2 < 2 || iso_samples_1 < 2 || iso_samples_2 < 2)
  2733.     int_error("samples or iso_samples < 2. Must be at least 2.", NO_CARET);
  2734.  
  2735.     if (this_plot->has_grid_topology && hidden3d) {
  2736.     xdiff = (x_max - x_min) / (iso_samples_1 - 1);
  2737.     ydiff = (y_max - y_min) / (iso_samples_2 - 1);
  2738.     } else {
  2739.     xdiff = (x_max - x_min) / (samples_1 - 1);
  2740.     ydiff = (y_max - y_min) / (samples_2 - 1);
  2741.     }
  2742.     xisodiff = (x_max - x_min) / (iso_samples_1 - 1);
  2743.     yisodiff = (y_max - y_min) / (iso_samples_2 - 1);
  2744.  
  2745.     this_plot = first_3dplot;
  2746.     c_token = begin_token;    /* start over */
  2747.  
  2748.     /* Read through functions */
  2749.     while (TRUE) {
  2750.     if (is_definition(c_token)) {
  2751.         define();
  2752.     } else {
  2753.         if (isstring(c_token)) {    /* data file to plot */
  2754.         /* ignore this now */
  2755.         c_token++;
  2756.         if (almost_equals(c_token, "i$ndex")) {
  2757.             struct value a;
  2758.             int index;
  2759.  
  2760.             c_token++;        /* skip "index" */
  2761.             index = (int) magnitude(const_express(&a));
  2762.         }
  2763.         if (almost_equals(c_token, "u$sing")) {
  2764.             c_token++;    /* skip "using" */
  2765.             if (!isstring(c_token)) {
  2766.             struct value    a;
  2767.             (void) magnitude(const_express(&a));    /* skip xcol */
  2768.             if (equals(c_token, ":")) {
  2769.                 c_token++;    /* skip ":" */
  2770.                 (void) magnitude(const_express(&a));    /* skip ycol */
  2771.                 if (equals(c_token, ":")) {
  2772.                 c_token++;    /* skip ":" */
  2773.                 (void) magnitude(const_express(&a));    /* skip zcol */
  2774.                 }
  2775.             }
  2776.             }
  2777.             if (isstring(c_token))
  2778.             c_token++;    /* skip format string */
  2779.         }
  2780.         } else {        /* function to plot */
  2781.         struct iso_curve *this_iso = this_plot->iso_crvs;
  2782.         struct coordinate GPHUGE *points = this_iso->points;
  2783.         int num_sam_to_use, num_iso_to_use;
  2784.  
  2785.         if (parametric)
  2786.             crnt_param = (crnt_param + 1) % 3;
  2787.         dummy_func = &plot_func;
  2788.         plot_func.at = temp_at();    /* reparse function */
  2789.  
  2790.         num_iso_to_use = iso_samples_2;
  2791.         if (!(this_plot->has_grid_topology && hidden3d))
  2792.             num_sam_to_use = samples_1;
  2793.         else
  2794.             num_sam_to_use = iso_samples_1;
  2795.  
  2796.         is_log_func=(!parametric)||(crnt_param==0)?is_log_z:(crnt_param==1?is_log_x:is_log_y);
  2797.         for (j = 0; j < num_iso_to_use; j++) {
  2798.             y = y_min + j * yisodiff;
  2799.             /* if (is_log_y) PEM fix logscale y axis */
  2800.             /* y = pow(log_base_log_y,y); 26-Sep-89 */
  2801.             (void) Gcomplex(&plot_func.dummy_values[1],
  2802.                    is_log_y ? pow(base_log_y, y) : y,
  2803.                    0.0);
  2804.  
  2805.             for (i = 0; i < num_sam_to_use; i++) {
  2806.             x = x_min + i * xdiff;
  2807.             /* if (is_log_x) PEM fix logscale x axis */
  2808.             /* x = pow(base_log_x,x); 26-Sep-89 */
  2809.             (void) Gcomplex(&plot_func.dummy_values[0],
  2810.                        is_log_x ? pow(base_log_x, x) : x,
  2811.                        0.0);
  2812.  
  2813.             points[i].x = x;
  2814.             points[i].y = y;
  2815.  
  2816.             evaluate_at(plot_func.at, &a);
  2817.  
  2818.             if (undefined || (fabs(imag(&a)) > zero)) {
  2819.                 points[i].type = UNDEFINED;
  2820.                 continue;
  2821.             }
  2822.             temp = real(&a);
  2823.  
  2824.             if (is_log_func && temp < 0.0) {
  2825.                 points[i].type = UNDEFINED;
  2826.                 continue;
  2827.             }
  2828.             if (is_log_func) {
  2829.                 if (temp == 0.0) {
  2830.                 points[i].type = OUTRANGE;
  2831.                 points[i].z = -VERYLARGE;
  2832.                 continue;
  2833.                 } else {
  2834.                 points[i].z = log(temp)/log_base_log_z;
  2835.                 }
  2836.             } else
  2837.                 points[i].z = temp;
  2838.  
  2839.             if (autoscale_lz || inrange(temp, zmin, zmax)) {
  2840.                 points[i].type = INRANGE;
  2841.                 if (autoscale_lz) {
  2842.                 if (temp < zmin)
  2843.                     zmin = temp;
  2844.                 if (temp > zmax)
  2845.                     zmax = temp;
  2846.                 }
  2847.             } else
  2848.                 points[i].type = OUTRANGE;
  2849.             }
  2850.             this_iso->p_count = num_sam_to_use;
  2851.             this_iso = this_iso->next;
  2852.             points = this_iso? this_iso->points: NULL;
  2853.         }
  2854.  
  2855.         if (!(this_plot->has_grid_topology && hidden3d)) {
  2856.             num_iso_to_use = iso_samples_1;
  2857.             num_sam_to_use = samples_2;
  2858.             is_log_func=(!parametric)||(crnt_param==0)?is_log_z:(crnt_param==1?is_log_x:is_log_y);
  2859.             for (i = 0; i < num_iso_to_use; i++) {
  2860.             x = x_min + i * xisodiff;
  2861.             /* if (is_log_x) PEM fix logscale x axis */
  2862.             /* x = pow(base_log_x,x); 26-Sep-89 */
  2863.             (void) Gcomplex(&plot_func.dummy_values[0],
  2864.                        is_log_x ? pow(base_log_x, x) : x,
  2865.                        0.0);
  2866.  
  2867.             for (j = 0; j < num_sam_to_use; j++) {
  2868.                 y = y_min + j * ydiff;
  2869.                 /* if (is_log_y) PEM fix logscale y axis */
  2870.                 /* y = pow(base_log_y,y); 26-Sep-89 */
  2871.                 (void) Gcomplex(&plot_func.dummy_values[1],
  2872.                        is_log_y ? pow(base_log_y, y) : y,
  2873.                        0.0);
  2874.  
  2875.                 points[j].x = x;
  2876.                 points[j].y = y;
  2877.  
  2878.                 evaluate_at(plot_func.at, &a);
  2879.  
  2880.                 if (undefined || (fabs(imag(&a)) > zero)) {
  2881.                 points[j].type = UNDEFINED;
  2882.                 continue;
  2883.                 }
  2884.                 temp = real(&a);
  2885.  
  2886.                 if (is_log_func && temp < 0.0) {
  2887.                 points[j].type = UNDEFINED;
  2888.                 continue;
  2889.                 }
  2890.                 if (is_log_func) {
  2891.                 if (temp == 0.0) {
  2892.                     points[j].type = OUTRANGE;
  2893.                     points[j].z = -VERYLARGE;
  2894.                     continue;
  2895.                 } else {
  2896.                     points[j].z = log(temp)/log_base_log_z;
  2897.                 }
  2898.                 } else
  2899.                 points[j].z = temp;
  2900.  
  2901.                 if (autoscale_lz
  2902.                 || inrange(temp, zmin, zmax)) {
  2903.                 points[j].type = INRANGE;
  2904.                 if (autoscale_lz) {
  2905.                     if (temp < zmin)
  2906.                     zmin = temp;
  2907.                     if (temp > zmax)
  2908.                     zmax = temp;
  2909.                 }
  2910.                 } else
  2911.                 points[j].type = OUTRANGE;
  2912.             }
  2913.             this_iso->p_count = num_sam_to_use;
  2914.             this_iso = this_iso->next;
  2915.             points = this_iso ? this_iso->points : NULL;
  2916.             }
  2917.         }
  2918.         }
  2919.  
  2920.         /* title was handled above */
  2921.         if (almost_equals(c_token, "t$itle")) {
  2922.         c_token++;
  2923.         c_token++;
  2924.         } else if (almost_equals(c_token, "not$itle")) {
  2925.             c_token++;
  2926.         }
  2927.         /* style was handled above */
  2928.         if (almost_equals(c_token, "w$ith")) {
  2929.         c_token++;
  2930.         c_token++;
  2931.         }
  2932.         /* line and point types were handled above */
  2933.         if (!equals(c_token, ",") && !END_OF_COMMAND) {
  2934.         struct value    t;
  2935.         (void) real(const_express(&t));
  2936.         }
  2937.         if (!equals(c_token, ",") && !END_OF_COMMAND) {
  2938.         struct value    t;
  2939.         (void) real(const_express(&t));
  2940.         }
  2941.         this_plot = this_plot->next_sp;
  2942.     }
  2943.  
  2944.     if (equals(c_token, ","))
  2945.         c_token++;
  2946.     else
  2947.         break;
  2948.     }
  2949.  
  2950.     if (fabs(zmax - zmin) < zero)
  2951.     /* if autoscale, widen range */
  2952.     if (autoscale_lz) {
  2953.         fprintf(stderr, "Warning: empty z range [%g:%g], ", zmin, zmax);
  2954.         if (fabs(zmin) < zero) {
  2955.         zmin = -1.;
  2956.         zmax = 1.;
  2957.         } else {
  2958.         /* expand range by 10% in either direction */
  2959.         zmin = zmin * 0.9;
  2960.         zmax = zmax * 1.1;
  2961.         }
  2962.         fprintf(stderr, "adjusting to [%g:%g]\n", zmin, zmax);
  2963.     } else {
  2964.         int_error("z range is less than `zero`", c_token);
  2965.     }
  2966.  
  2967.     /* Now we finally know the real zmin and zmax */
  2968.     if (is_log_z) {
  2969.     if (zmin <= 0.0 || zmax <= 0.0)
  2970.         int_error("z range must be greater than 0 for log scale!", NO_CARET);
  2971.     z_min = log(zmin)/log_base_log_z;
  2972.     z_max = log(zmax)/log_base_log_z;
  2973.     } else {
  2974.     z_min = zmin;
  2975.     z_max = zmax;
  2976.     }
  2977.     capture(replot_line, plot_token, c_token);
  2978.  
  2979.     if (parametric) {
  2980.     /* Now put u/v and x/y ranges back before we actually plot anything. */
  2981.     ltmp = autoscale_lx;
  2982.     autoscale_lx = autoscale_lu;
  2983.     autoscale_lu = ltmp;
  2984.     ltmp = autoscale_ly;
  2985.     autoscale_ly = autoscale_lv;
  2986.     autoscale_lv = ltmp;
  2987.     temp = xmin;
  2988.     xmin = umin;
  2989.     umin = temp;
  2990.     temp = xmax;
  2991.     xmax = umax;
  2992.     umax = temp;
  2993.     temp = ymin;
  2994.     ymin = vmin;
  2995.     vmin = temp;
  2996.     temp = ymax;
  2997.     ymax = vmax;
  2998.     vmax = temp;
  2999.  
  3000.     /* Now actually fix the plot triplets to be single plots. */
  3001.     parametric_3dfixup(first_3dplot, &plot_num,
  3002.                &x_min, &x_max, &y_min, &y_max,
  3003.                &z_min, &z_max);
  3004.     if (is_log_x) {
  3005.         if (x_min <= 0.0 || x_max <= 0.0)
  3006.         int_error("x range must be greater than 0 for log scale!", NO_CARET);
  3007.         x_min = log(x_min)/log_base_log_x;
  3008.         x_max = log(x_max)/log_base_log_x;
  3009.     }
  3010.     if (is_log_y) {
  3011.         if (y_min <= 0.0 || y_max <= 0.0)
  3012.         int_error("y range must be greater than 0 for log scale!", NO_CARET);
  3013.         y_min = log(y_min)/log_base_log_y;
  3014.         y_max = log(y_max)/log_base_log_y;
  3015.     }
  3016.     if (is_log_z) {
  3017.         if (z_min <= 0.0 || z_max <= 0.0)
  3018.         int_error("z range must be greater than 0 for log scale!", NO_CARET);
  3019.         z_min = log(z_min)/log_base_log_z;
  3020.         z_max = log(z_max)/log_base_log_z;
  3021.     }
  3022.     }
  3023.  
  3024.     /* Filter out empty meshes. */
  3025.     while (first_3dplot &&
  3026.        first_3dplot->num_iso_read == 0 &&
  3027.        first_3dplot->plot_type == DATA3D) {
  3028.     struct surface_points *plt = first_3dplot->next_sp;
  3029.  
  3030.     first_3dplot->next_sp = NULL;
  3031.     sp_free(first_3dplot);
  3032.     plot_num--;
  3033.     first_3dplot = plt;
  3034.     }
  3035.     if (first_3dplot != NULL) {
  3036.     struct surface_points *plt1, *plt2;
  3037.     
  3038.     for (plt1 = first_3dplot, plt2 = plt1->next_sp; plt2 != NULL; ) {
  3039.         if (plt2->num_iso_read == 0 && plt2->plot_type == DATA3D) {
  3040.         plt2 = plt2->next_sp;
  3041.         plt1->next_sp->next_sp = NULL;
  3042.         sp_free(plt1->next_sp);
  3043.         plot_num--;
  3044.         plt1->next_sp = plt2;
  3045.         }
  3046.         else {
  3047.         plt1 = plt2;
  3048.         plt2 = plt2->next_sp;
  3049.         }
  3050.     }
  3051.     }
  3052.     if (first_3dplot == NULL)
  3053.     int_error("no data found in file", NO_CARET);
  3054.  
  3055.     /* Creates contours if contours are to be plotted as well. */
  3056.     if (draw_contour) {
  3057.     for (this_plot = first_3dplot, i = 0;
  3058.          i < plot_num;
  3059.          this_plot = this_plot->next_sp, i++) {
  3060.         if (this_plot->contours) {
  3061.         struct gnuplot_contours *cntr, *cntrs = this_plot->contours;
  3062.  
  3063.         while (cntrs) {
  3064.             cntr = cntrs;
  3065.             cntrs = cntrs->next;
  3066.             gpfarfree(cntr->coords);
  3067.             free(cntr);
  3068.         }
  3069.         }
  3070.         /* Make sure this one can be contoured. */
  3071.         if (!this_plot->has_grid_topology) {
  3072.         this_plot->contours = NULL;
  3073.         fprintf(stderr,"Notice: cannot contour non grid data!\n");
  3074.         /* changed from int_error by recommendation of rkc@xn.ll.mit.edu */
  3075.         }
  3076.         else if (this_plot->plot_type == DATA3D)
  3077.         this_plot->contours = contour(
  3078.                          this_plot->num_iso_read,
  3079.                          this_plot->iso_crvs,
  3080.                          contour_levels, contour_pts,
  3081.                          contour_kind, contour_order,
  3082.                          levels_kind, levels_list);
  3083.         else
  3084.         this_plot->contours = contour(iso_samples_2,
  3085.                           this_plot->iso_crvs,
  3086.                           contour_levels, contour_pts,
  3087.                           contour_kind, contour_order,
  3088.                           levels_kind, levels_list);
  3089.     }
  3090.     }
  3091.     if (strcmp(term_tbl[term].name, "table") == 0)
  3092.     print_3dtable();
  3093.     else
  3094.     do_3dplot(first_3dplot, plot_num, x_min, x_max, y_min, y_max, z_min, z_max);
  3095.     sp_free(first_3dplot);
  3096.     first_3dplot = NULL;
  3097. }
  3098.  
  3099. done(status)
  3100.     int             status;
  3101. {
  3102.     if (term && term_init)
  3103.     (*term_tbl[term].reset) ();
  3104. #ifdef vms
  3105.     vms_reset();
  3106. #endif
  3107.     exit(status);
  3108. }
  3109.  
  3110. void 
  3111. parametric_fixup(start_plot, plot_num, x_min, x_max)
  3112.     struct curve_points *start_plot;
  3113.     int            *plot_num;
  3114.     double         *x_min, *x_max;
  3115. /*
  3116.  * The hardest part of this routine is collapsing the FUNC plot types in the
  3117.  * list (which are gauranteed to occur in (x,y) pairs while preserving the
  3118.  * non-FUNC type plots intact.  This means we have to work our way through
  3119.  * various lists.  Examples (hand checked): start_plot:F1->F2->NULL ==>
  3120.  * F2->NULL start_plot:F1->F2->F3->F4->F5->F6->NULL ==> F2->F4->F6->NULL
  3121.  * start_plot:F1->F2->D1->D2->F3->F4->D3->NULL ==> F2->D1->D2->F4->D3->NULL
  3122.  * 
  3123.  * Of course, the more interesting work is to move the y values of the x
  3124.  * function to become the x values of the y function (checking the mins and
  3125.  * maxs as we go along).
  3126.  */
  3127. {
  3128.     struct curve_points *xp, *new_list, *yp = start_plot, *tmp, *free_list,
  3129.                    *free_head = NULL;
  3130.     int             i, tlen, curve;
  3131.     char           *new_title;
  3132.     double          lxmin, lxmax, temp;
  3133.  
  3134.     if (autoscale_lx) {
  3135.     lxmin = VERYLARGE;
  3136.     lxmax = -VERYLARGE;
  3137.     } else {
  3138.     lxmin = xmin;
  3139.     lxmax = xmax;
  3140.     }
  3141.  
  3142.     /*
  3143.      * Ok, go through all the plots and move FUNC types together.  Note: this
  3144.      * originally was written to look for a NULL next pointer, but gnuplot
  3145.      * wants to be sticky in grabbing memory and the right number of items in
  3146.      * the plot list is controlled by the plot_num variable.
  3147.      * 
  3148.      * Since gnuplot wants to do this sticky business, a free_list of
  3149.      * curve_points is kept and then tagged onto the end of the plot list as
  3150.      * this seems more in the spirit of the original memory behavior than
  3151.      * simply freeing the memory.  I'm personally not convinced this sort of
  3152.      * concern is worth it since the time spent computing points seems to
  3153.      * dominate any garbage collecting that might be saved here...
  3154.      */
  3155.     new_list = xp = start_plot;
  3156.     yp = xp->next_cp;
  3157.     curve = 0;
  3158.  
  3159.     for (; curve < *plot_num; xp = xp->next_cp, yp = yp ? yp->next_cp : yp, curve++) {
  3160.     if (xp->plot_type != FUNC) {
  3161.         continue;
  3162.     }
  3163.     /* Here's a FUNC parametric function defined as two parts. */
  3164.     --(*plot_num);
  3165.     /*
  3166.      * Go through all the points assigning the y's from xp to be the x's
  3167.      * for yp.  Check max's and min's as you go.
  3168.      */
  3169.     for (i = 0; i < yp->p_count; ++i) {
  3170.         /*
  3171.          * Throw away excess xp points, mark excess yp points as
  3172.          * OUTRANGE.
  3173.          */
  3174.         if (i > xp->p_count) {
  3175.         yp->points[i].type = OUTRANGE;
  3176.         continue;
  3177.         }
  3178.         /*
  3179.          * Just as we had to do when we computed y values--now check that
  3180.          * x's (computed parametrically) are in the permitted ranges as
  3181.          * well.
  3182.          */
  3183.         temp = xp->points[i].y;    /* New x value for yp function. */
  3184.         yp->points[i].x = temp;
  3185.         /* Handle undefined values differently from normal ranges. */
  3186.         if (xp->points[i].type == UNDEFINED)
  3187.         yp->points[i].type = xp->points[i].type;
  3188.         if (autoscale_lx || polar
  3189.         || inrange(temp, lxmin, lxmax)) {
  3190.         if (autoscale_lx && temp < lxmin)
  3191.             lxmin = temp;
  3192.         if (autoscale_lx && temp > lxmax)
  3193.             lxmax = temp;
  3194.         } else
  3195.         yp->points[i].type = OUTRANGE;    /* Due to x value. */
  3196.     }
  3197.     /* Ok, fix up the title to include both the xp and yp plots. */
  3198.     if (xp->title && xp->title[0] != '\0') {
  3199.         tlen = strlen(yp->title) + strlen(xp->title) + 3;
  3200.         new_title = alloc((unsigned long) tlen, "string");
  3201.         strcpy(new_title, xp->title);
  3202.         strcat(new_title, ", ");    /* + 2 */
  3203.         strcat(new_title, yp->title);    /* + 1 = + 3 */
  3204.         free(yp->title);
  3205.         yp->title = new_title;
  3206.     }
  3207.     /* Eliminate the first curve (xparam) and just use the second. */
  3208.     if (xp == start_plot) {
  3209.         /* Simply nip off the first element of the list. */
  3210.         new_list = first_plot = yp;
  3211.         xp = xp->next_cp;
  3212.         if (yp->next_cp != NULL)
  3213.         yp = yp->next_cp;
  3214.         /* Add start_plot to the free_list. */
  3215.         if (free_head == NULL) {
  3216.         free_list = free_head = start_plot;
  3217.         free_head->next_cp = NULL;
  3218.         } else {
  3219.         free_list->next_cp = start_plot;
  3220.         start_plot->next_cp = NULL;
  3221.         free_list = start_plot;
  3222.         }
  3223.     } else {
  3224.         /* Here, remove the xp node and replace it with the yp node. */
  3225.         tmp = xp;
  3226.         /* Pass over any data files that might have been in place. */
  3227.         while (new_list->next_cp && new_list->next_cp != xp)
  3228.         new_list = new_list->next_cp;
  3229.         new_list->next_cp = yp;
  3230.         new_list = new_list->next_cp;
  3231.         xp = xp->next_cp;
  3232.         if (yp->next_cp != NULL)
  3233.         yp = yp->next_cp;
  3234.         /* Add tmp to the free_list. */
  3235.         tmp->next_cp = NULL;
  3236.         if (free_head == NULL) {
  3237.         free_list = free_head = tmp;
  3238.         } else {
  3239.         free_list->next_cp = tmp;
  3240.         free_list = tmp;
  3241.         }
  3242.     }
  3243.     }
  3244.     /* Ok, stick the free list at the end of the curve_points plot list. */
  3245.     while (new_list->next_cp != NULL)
  3246.     new_list = new_list->next_cp;
  3247.     new_list->next_cp = free_head;
  3248.  
  3249.     /* Report the overall graph mins and maxs. */
  3250.     *x_min = lxmin;
  3251.     *x_max = lxmax;
  3252. }
  3253.  
  3254. void 
  3255. parametric_3dfixup(start_plot, plot_num, x_min, x_max, y_min, y_max,
  3256.            z_min, z_max)
  3257.     struct surface_points *start_plot;
  3258.     int            *plot_num;
  3259.     double         *x_min, *x_max, *y_min, *y_max, *z_min, *z_max;
  3260. /*
  3261.  * The hardest part of this routine is collapsing the FUNC plot types in the
  3262.  * list (which are gauranteed to occur in (x,y,z) triplets while preserving
  3263.  * the non-FUNC type plots intact.  This means we have to work our way
  3264.  * through various lists.  Examples (hand checked):
  3265.  * start_plot:F1->F2->F3->NULL ==> F3->NULL
  3266.  * start_plot:F1->F2->F3->F4->F5->F6->NULL ==> F3->F6->NULL
  3267.  * start_plot:F1->F2->F3->D1->D2->F4->F5->F6->D3->NULL ==>
  3268.  * F3->D1->D2->F6->D3->NULL
  3269.  */
  3270. {
  3271.     struct surface_points *xp, *yp, *zp, *new_list, *tmp, *free_list, *free_head = NULL;
  3272.     struct iso_curve *icrvs, *xicrvs, *yicrvs, *zicrvs;
  3273.     int             i, tlen, surface;
  3274.     char           *new_title;
  3275.     double          lxmin, lxmax, lymin, lymax, lzmin, lzmax;
  3276.  
  3277.     if (autoscale_lx) {
  3278.     lxmin = VERYLARGE;
  3279.     lxmax = -VERYLARGE;
  3280.     } else {
  3281.     lxmin = xmin;
  3282.     lxmax = xmax;
  3283.     }
  3284.  
  3285.     if (autoscale_ly) {
  3286.     lymin = VERYLARGE;
  3287.     lymax = -VERYLARGE;
  3288.     } else {
  3289.     lymin = ymin;
  3290.     lymax = ymax;
  3291.     }
  3292.  
  3293.     if (autoscale_lz) {
  3294.     lzmin = VERYLARGE;
  3295.     lzmax = -VERYLARGE;
  3296.     } else {
  3297.     lzmin = zmin;
  3298.     lzmax = zmax;
  3299.     }
  3300.  
  3301.     /*
  3302.      * Ok, go through all the plots and move FUNC3D types together.  Note:
  3303.      * this originally was written to look for a NULL next pointer, but
  3304.      * gnuplot wants to be sticky in grabbing memory and the right number of
  3305.      * items in the plot list is controlled by the plot_num variable.
  3306.      * 
  3307.      * Since gnuplot wants to do this sticky business, a free_list of
  3308.      * surface_points is kept and then tagged onto the end of the plot list
  3309.      * as this seems more in the spirit of the original memory behavior than
  3310.      * simply freeing the memory.  I'm personally not convinced this sort of
  3311.      * concern is worth it since the time spent computing points seems to
  3312.      * dominate any garbage collecting that might be saved here...
  3313.      */
  3314.     new_list = xp = start_plot;
  3315.     for (surface = 0; surface < *plot_num; surface++) {
  3316.     if (xp->plot_type != FUNC3D) {
  3317.         icrvs = xp->iso_crvs;
  3318.  
  3319.         while (icrvs) {
  3320.         struct coordinate GPHUGE *points = icrvs->points;
  3321.  
  3322.         for (i = 0; i < icrvs->p_count; ++i) {
  3323.             if (lxmin > points[i].x)
  3324.             lxmin = points[i].x;
  3325.             if (lxmax < points[i].x)
  3326.             lxmax = points[i].x;
  3327.             if (lymin > points[i].y)
  3328.             lymin = points[i].y;
  3329.             if (lymax < points[i].y)
  3330.             lymax = points[i].y;
  3331.             if (lzmin > points[i].z)
  3332.             lzmin = points[i].z;
  3333.             if (lzmax < points[i].z)
  3334.             lzmax = points[i].z;
  3335.         }
  3336.  
  3337.         icrvs = icrvs->next;
  3338.         }
  3339.         xp = xp->next_sp;
  3340.         continue;
  3341.     }
  3342.     yp = xp->next_sp;
  3343.     zp = yp->next_sp;
  3344.  
  3345.     /* Here's a FUNC3D parametric function defined as three parts. */
  3346.     (*plot_num) -= 2;
  3347.     /*
  3348.      * Go through all the points and assign the x's and y's from xp and
  3349.      * yp to zp.  Check max's and min's as you go.
  3350.      */
  3351.     xicrvs = xp->iso_crvs;
  3352.     yicrvs = yp->iso_crvs;
  3353.     zicrvs = zp->iso_crvs;
  3354.     while (zicrvs) {
  3355.         struct coordinate GPHUGE *xpoints = xicrvs->points, GPHUGE *ypoints = yicrvs->points, GPHUGE *zpoints = zicrvs->points;
  3356.         for (i = 0; i < zicrvs->p_count; ++i) {
  3357.         zpoints[i].x = xpoints[i].z;
  3358.         zpoints[i].y = ypoints[i].z;
  3359.  
  3360.         if (lxmin > zpoints[i].x)
  3361.             lxmin = zpoints[i].x;
  3362.         if (lxmax < zpoints[i].x)
  3363.             lxmax = zpoints[i].x;
  3364.         if (lymin > zpoints[i].y)
  3365.             lymin = zpoints[i].y;
  3366.         if (lymax < zpoints[i].y)
  3367.             lymax = zpoints[i].y;
  3368.         if (lzmin > zpoints[i].z)
  3369.             lzmin = zpoints[i].z;
  3370.         if (lzmax < zpoints[i].z)
  3371.             lzmax = zpoints[i].z;
  3372.         }
  3373.         xicrvs = xicrvs->next;
  3374.         yicrvs = yicrvs->next;
  3375.         zicrvs = zicrvs->next;
  3376.     }
  3377.  
  3378.     /* Ok, fix up the title to include xp and yp plots. */
  3379.     if ((xp->title && xp->title[0] != '\0') ||
  3380.         (yp->title && yp->title[0] != '\0')) {
  3381.         tlen = (xp->title ? strlen(xp->title) : 0) +
  3382.         (yp->title ? strlen(yp->title) : 0) +
  3383.         (zp->title ? strlen(zp->title) : 0) + 5;
  3384.         new_title = alloc((unsigned long) tlen, "string");
  3385.         new_title[0] = 0;
  3386.         if (xp->title) {
  3387.         strcat(new_title, xp->title);
  3388.         strcat(new_title, ", ");    /* + 2 */
  3389.         }
  3390.         if (yp->title) {
  3391.         strcat(new_title, yp->title);
  3392.         strcat(new_title, ", ");    /* + 2 */
  3393.         }
  3394.         if (zp->title) {
  3395.         strcat(new_title, zp->title);
  3396.         }
  3397.         free(zp->title);
  3398.         zp->title = new_title;
  3399.     }
  3400.     /*
  3401.      * Eliminate the first two surfaces (xp and yp) and just use the
  3402.      * third.
  3403.      */
  3404.     if (xp == start_plot) {
  3405.         /* Simply nip off the first two elements of the list. */
  3406.         new_list = first_3dplot = zp;
  3407.         xp = zp->next_sp;
  3408.         /* Add xp and yp to the free_list. */
  3409.         if (free_head == NULL) {
  3410.         free_head = start_plot;
  3411.         } else {
  3412.         free_list->next_sp = start_plot;
  3413.         }
  3414.         free_list = start_plot->next_sp;
  3415.         free_list->next_sp = NULL;
  3416.     } else {
  3417.         /*
  3418.          * Here, remove the xp,yp nodes and replace them with the zp
  3419.          * node.
  3420.          */
  3421.         tmp = xp;
  3422.         /* Pass over any data files that might have been in place. */
  3423.         while (new_list->next_sp && new_list->next_sp != xp)
  3424.         new_list = new_list->next_sp;
  3425.         new_list->next_sp = zp;
  3426.         new_list = zp;
  3427.         xp = zp->next_sp;
  3428.         /* Add tmp to the free_list. */
  3429.         if (free_head == NULL) {
  3430.         free_head = tmp;
  3431.         } else {
  3432.         free_list->next_sp = tmp;
  3433.         }
  3434.         free_list = tmp->next_sp;
  3435.         free_list->next_sp = NULL;
  3436.     }
  3437.     }
  3438.     /* Ok, stick the free list at the end of the surface_points plot list. */
  3439.     while (new_list->next_sp != NULL)
  3440.     new_list = new_list->next_sp;
  3441.     new_list->next_sp = free_head;
  3442.     if (lxmax - lxmin < zero) {
  3443.     if (fabs(lxmax) < zero) {
  3444.         lxmin = -1.0;
  3445.         lxmax = 1.0;
  3446.     } else {
  3447.         lxmin *= 0.9;
  3448.         lxmax *= 1.1;
  3449.     }
  3450.     }
  3451.     if (lymax - lymin < zero) {
  3452.     if (fabs(lymax) < zero) {
  3453.         lymin = -1.0;
  3454.         lymax = 1.0;
  3455.     } else {
  3456.         lymin *= 0.9;
  3457.         lymax *= 1.1;
  3458.     }
  3459.     }
  3460.     if (lzmax - lzmin < zero) {
  3461.     if (fabs(lzmax) < zero) {
  3462.         lzmin = -1.0;
  3463.         lzmax = 1.0;
  3464.     } else {
  3465.         lzmin *= 0.9;
  3466.         lzmax *= 1.1;
  3467.     }
  3468.     }
  3469.     /* Report the overall graph mins and maxs. */
  3470.     if (autoscale_lx) {
  3471.     *x_min = (is_log_x ? pow(base_log_x, lxmin) : lxmin);
  3472.     *x_max = (is_log_x ? pow(base_log_x, lxmax) : lxmax);
  3473.     } else {
  3474.     *x_min = xmin;
  3475.     *x_max = xmax;
  3476.     }
  3477.     if (autoscale_ly) {
  3478.     *y_min = (is_log_y ? pow(base_log_y, lymin) : lymin);
  3479.     *y_max = (is_log_y ? pow(base_log_y, lymax) : lymax);
  3480.     } else {
  3481.     *y_min = ymin;
  3482.     *y_max = ymax;
  3483.     }
  3484.     if (autoscale_lz) {
  3485.     *z_min = (is_log_z ? pow(base_log_z, lzmin) : lzmin);
  3486.     *z_max = (is_log_z ? pow(base_log_z, lzmax) : lzmax);
  3487.     } else {
  3488.     *z_min = zmin;
  3489.     *z_max = zmax;
  3490.     }
  3491. }
  3492.  
  3493. #ifdef AMIGA_SC_6_1
  3494. void 
  3495. sleep(delay)
  3496.     unsigned int    delay;
  3497. {
  3498.     Delay(50 * delay);
  3499. }
  3500. #endif
  3501.  
  3502. #ifdef AMIGA_AC_5
  3503. void 
  3504. sleep(delay)
  3505.     unsigned int    delay;
  3506. {
  3507.     unsigned long   time_is_up;
  3508.     time_is_up = time(NULL) + (unsigned long) delay;
  3509.     while (time(NULL) < time_is_up)
  3510.      /* wait */ ;
  3511. }
  3512. #endif
  3513.  
  3514. #if defined(MSDOS) || defined(_Windows) || defined(DOS386)
  3515. #if (!defined(__TURBOC__) && !defined(__EMX__) && !defined(DJGPP)) || defined(_Windows) /* Turbo C already has sleep() */
  3516. #ifndef __ZTC__            /* ZTC already has usleep() */
  3517. /* kludge to provide sleep() for msc 5.1 */
  3518. void 
  3519. sleep(delay)
  3520.     unsigned int    delay;
  3521. {
  3522.     unsigned long   time_is_up;
  3523.     time_is_up = time(NULL) + (unsigned long) delay;
  3524.     while (time(NULL) < time_is_up)
  3525.      /* wait */ ;
  3526. }
  3527. #endif                /* not ZTC */
  3528. #endif                /* (!TURBOC && !__EMX__ && !DJGPP) or _Windows */
  3529. #endif                /* MSDOS || _Windows*/
  3530.  
  3531.  
  3532. /* Support for input, shell, and help for various systems */
  3533.  
  3534. #ifdef vms
  3535.  
  3536. #include <descrip.h>
  3537. #include <rmsdef.h>
  3538. #include <errno.h>
  3539. #include <smgdef.h>
  3540. #include <smgmsg.h>
  3541.  
  3542. extern          lib$get_input(), lib$put_output();
  3543. extern          smg$read_composed_line();
  3544.  
  3545. int             vms_len;
  3546.  
  3547. unsigned int    status[2] =
  3548. {1, 0};
  3549.  
  3550. static char     help[MAX_LINE_LEN + 1] = "gnuplot";
  3551.  
  3552. $DESCRIPTOR(prompt_desc, PROMPT);
  3553. $DESCRIPTOR(line_desc, input_line);
  3554.  
  3555. $DESCRIPTOR(help_desc, help);
  3556. $DESCRIPTOR(helpfile_desc, "GNUPLOT$HELP");
  3557.  
  3558.  
  3559. read_line(prompt)
  3560.     char           *prompt;
  3561. {
  3562.     int             more, start = 0;
  3563.     char            expand_prompt[40];
  3564.  
  3565.     prompt_desc.dsc$w_length = strlen(prompt);
  3566.     prompt_desc.dsc$a_pointer = prompt;
  3567.     (void) strcpy(expand_prompt, "_");
  3568.     (void) strncat(expand_prompt, prompt, 38);
  3569.     do {
  3570.     line_desc.dsc$w_length = MAX_LINE_LEN - start;
  3571.     line_desc.dsc$a_pointer = &input_line[start];
  3572.     switch (status[1] = smg$read_composed_line(&vms_vkid, 0, &line_desc, &prompt_desc, &vms_len)) {
  3573.     case SMG$_EOF:
  3574.         done(IO_SUCCESS);    /* ^Z isn't really an error */
  3575.         break;
  3576.     case RMS$_TNS:        /* didn't press return in time */
  3577.         vms_len--;        /* skip the last character */
  3578.         break;        /* and parse anyway */
  3579.     case RMS$_BES:        /* Bad Escape Sequence */
  3580.     case RMS$_PES:        /* Partial Escape Sequence */
  3581.         sys$putmsg(status);
  3582.         vms_len = 0;    /* ignore the line */
  3583.         break;
  3584.     case SS$_NORMAL:
  3585.         break;        /* everything's fine */
  3586.     default:
  3587.         done(status[1]);    /* give the error message */
  3588.     }
  3589.     start += vms_len;
  3590.     input_line[start] = '\0';
  3591.     inline_num++;
  3592.     if (input_line[start - 1] == '\\') {
  3593.         /* Allow for a continuation line. */
  3594.         prompt_desc.dsc$w_length = strlen(expand_prompt);
  3595.         prompt_desc.dsc$a_pointer = expand_prompt;
  3596.         more = 1;
  3597.         --start;
  3598.     } else {
  3599.         line_desc.dsc$w_length = strlen(input_line);
  3600.         line_desc.dsc$a_pointer = input_line;
  3601.         more = 0;
  3602.     }
  3603.     } while (more);
  3604.     return 0;
  3605. }
  3606.  
  3607.  
  3608. do_help()
  3609. {
  3610.     help_desc.dsc$w_length = strlen(help);
  3611.     if ((vaxc$errno = lbr$output_help(lib$put_output, 0, &help_desc,
  3612.                &helpfile_desc, 0, lib$get_input)) != SS$_NORMAL)
  3613.     os_error("can't open GNUPLOT$HELP", NO_CARET);
  3614. }
  3615.  
  3616.  
  3617. do_shell()
  3618. {
  3619.     if ((vaxc$errno = lib$spawn()) != SS$_NORMAL) {
  3620.     os_error("spawn error", NO_CARET);
  3621.     }
  3622. }
  3623.  
  3624.  
  3625. do_system()
  3626. {
  3627.     input_line[0] = ' ';    /* an embarrassment, but... */
  3628.  
  3629.     if ((vaxc$errno = lib$spawn(&line_desc)) != SS$_NORMAL)
  3630.     os_error("spawn error", NO_CARET);
  3631.  
  3632.     (void) putc('\n', stderr);
  3633. }
  3634.  
  3635. #else                /* vms */
  3636.  
  3637. #ifdef _Windows
  3638. do_help()
  3639. {
  3640.     if (END_OF_COMMAND)
  3641.         WinHelp(textwin.hWndParent,(LPSTR)winhelpname,HELP_INDEX,(DWORD)NULL);
  3642.     else {
  3643.         char buf[128];
  3644.         int start = c_token++;
  3645.         while (!(END_OF_COMMAND))
  3646.             c_token++;
  3647.         capture(buf, start, c_token-1);
  3648.         WinHelp(textwin.hWndParent,(LPSTR)winhelpname,HELP_PARTIALKEY,(DWORD)buf);
  3649.     }
  3650. }
  3651. #else
  3652.  
  3653. /*
  3654.  * do_help: (not VMS, although it would work) Give help to the user. It
  3655.  * parses the command line into helpbuf and supplies help for that string.
  3656.  * Then, if there are subtopics available for that key, it prompts the user
  3657.  * with this string. If more input is given, do_help is called recursively,
  3658.  * with the argument the index of null character in the string. Thus a more
  3659.  * specific help can be supplied. This can be done repeatedly. If null input
  3660.  * is given, the function returns, effecting a backward climb up the tree.
  3661.  * David Kotz (David.Kotz@Dartmouth.edu) 10/89
  3662.  */
  3663.  
  3664. do_help()
  3665. {
  3666.     static char    *helpbuf = NULL;
  3667.     static char    *prompt = NULL;
  3668.     int             base;    /* index of first char AFTER help string */
  3669.     int             len;    /* length of current help string */
  3670.     TBOOLEAN         more_help;
  3671.     TBOOLEAN         only;    /* TRUE if only printing subtopics */
  3672.     int             subtopics;    /* 0 if no subtopics for this topic */
  3673.     int             start;    /* starting token of help string */
  3674.     char           *help_ptr;    /* name of help file */
  3675. #ifdef ATARI
  3676.     static char    help_fname[256]=""; /* keep helpfilename across calls */
  3677. #endif
  3678.  
  3679.     if ((help_ptr = getenv("GNUHELP")) == (char *) NULL)
  3680. #ifndef ATARI
  3681.     /* if can't find environment variable then just use HELPFILE */
  3682.     help_ptr = HELPFILE;
  3683. #else
  3684.     /* try whether we can find the helpfile via shell_find. If not, just
  3685.        use the default. (tnx Andreas) */
  3686.  
  3687. #ifdef sequent
  3688.     if( !index( HELPFILE, ':' ) && !index( HELPFILE, '/' ) &&
  3689.         !index( HELPFILE, '\\' ) ) {
  3690. #else
  3691.     if( !strchr( HELPFILE, ':' ) && !strchr( HELPFILE, '/' ) &&
  3692.         !strchr( HELPFILE, '\\' ) ) {
  3693. #endif
  3694.         if( strlen(help_fname)==0 ) {
  3695.         strcpy( help_fname, HELPFILE );
  3696.         if( shel_find( help_fname )==0 ) {
  3697.             strcpy( help_fname, HELPFILE );
  3698.         }
  3699.         }
  3700.         help_ptr=help_fname;
  3701.     } else {
  3702.         help_ptr=HELPFILE;
  3703.     }
  3704. #endif /* ATARI */
  3705.  
  3706.     /* Since MSDOS DGROUP segment is being overflowed we can not allow such  */
  3707.     /* huge static variables (1k each). Instead we dynamically allocate them */
  3708.     /* on the first call to this function...                     */
  3709.     if (helpbuf == NULL) {
  3710.     helpbuf = alloc((unsigned long)MAX_LINE_LEN, "help buffer");
  3711.     prompt = alloc((unsigned long)MAX_LINE_LEN, "help prompt");
  3712.     helpbuf[0] = prompt[0] = 0;
  3713.     }
  3714.     len = base = strlen(helpbuf);
  3715.  
  3716.     /* find the end of the help command */
  3717.     for (start = c_token; !(END_OF_COMMAND); c_token++);
  3718.     /* copy new help input into helpbuf */
  3719.     if (len > 0)
  3720.     helpbuf[len++] = ' ';    /* add a space */
  3721.     capture(helpbuf + len, start, c_token - 1);
  3722.     squash_spaces(helpbuf + base);    /* only bother with new stuff */
  3723.     lower_case(helpbuf + base);    /* only bother with new stuff */
  3724.     len = strlen(helpbuf);
  3725.  
  3726.     /* now, a lone ? will print subtopics only */
  3727.     if (strcmp(helpbuf + (base ? base + 1 : 0), "?") == 0) {
  3728.     /* subtopics only */
  3729.     subtopics = 1;
  3730.     only = TRUE;
  3731.     helpbuf[base] = '\0';    /* cut off question mark */
  3732.     } else {
  3733.     /* normal help request */
  3734.     subtopics = 0;
  3735.     only = FALSE;
  3736.     }
  3737.  
  3738.     switch (help(helpbuf, help_ptr, &subtopics)) {
  3739.     case H_FOUND:{
  3740.         /* already printed the help info */
  3741.         /* subtopics now is true if there were any subtopics */
  3742.         screen_ok = FALSE;
  3743.  
  3744.         do {
  3745.         if (subtopics && !only) {
  3746.             /* prompt for subtopic with current help string */
  3747.             if (len > 0)
  3748.             (void) sprintf(prompt, "Subtopic of %s: ", helpbuf);
  3749.             else
  3750.             (void) strcpy(prompt, "Help topic: ");
  3751.             read_line(prompt);
  3752.             num_tokens = scanner(input_line);
  3753.             c_token = 0;
  3754.             more_help = !(END_OF_COMMAND);
  3755.             if (more_help)
  3756.             /* base for next level is all of current helpbuf */
  3757.             do_help();
  3758.         } else
  3759.             more_help = FALSE;
  3760.         } while (more_help);
  3761.  
  3762.         break;
  3763.     }
  3764.     case H_NOTFOUND:{
  3765.         printf("Sorry, no help for '%s'\n", helpbuf);
  3766.         break;
  3767.     }
  3768.     case H_ERROR:{
  3769.         perror(help_ptr);
  3770.         break;
  3771.     }
  3772.     default:{            /* defensive programming */
  3773.         int_error("Impossible case in switch", NO_CARET);
  3774.         /* NOTREACHED */
  3775.     }
  3776.     }
  3777.  
  3778.     helpbuf[base] = '\0';    /* cut it off where we started */
  3779. }
  3780. #endif  /* _Windows */
  3781.  
  3782. #ifdef AMIGA_AC_5
  3783. char            strg0[256];
  3784. #endif
  3785.  
  3786. do_system()
  3787. {
  3788. #ifdef AMIGA_AC_5
  3789.     char           *parms[80];
  3790.     void            getparms();
  3791.  
  3792.     getparms(input_line + 1, parms);
  3793.     if (fexecv(parms[0], parms) < 0)
  3794. #else
  3795. #if defined(ATARI)&&defined(__GNUC__)
  3796.     /* use preloaded shell, if available */
  3797.     short           (*shell_p) (char *command);
  3798.     void           *ssp;
  3799.  
  3800.     ssp = (void *) Super(NULL);
  3801.     shell_p = *(short (**) (char *)) 0x4f6;
  3802.     Super(ssp);
  3803.  
  3804.     /* this is a bit strange, but we have to have a single if */
  3805.     if ((shell_p ? (*shell_p) (input_line + 1) : system(input_line + 1)))
  3806. #else
  3807. #ifdef _Windows
  3808.     if (winsystem(input_line + 1))
  3809. #else
  3810.     if (system(input_line + 1))
  3811. #endif
  3812. #endif
  3813. #endif
  3814.     os_error("system() failed", NO_CARET);
  3815. }
  3816.  
  3817. #ifdef AMIGA_AC_5
  3818.  
  3819. /******************************************************************************/
  3820. /* */
  3821. /* Parses the command string (for fexecv use) and  converts the first token  */
  3822. /* to lower case                                                          */
  3823. /* */
  3824. /******************************************************************************/
  3825.  
  3826. void 
  3827. getparms(command, parms)
  3828.     char           *command;
  3829.     char          **parms;
  3830. {
  3831.     register int    i = 0;    /* A bunch of indices          */
  3832.     register int    j = 0;
  3833.     register int    k = 0;
  3834.  
  3835.     while (*(command + j) != '\0') {    /* Loop on string characters   */
  3836.     parms[k++] = strg0 + i;
  3837.     while (*(command + j) == ' ')
  3838.         ++j;
  3839.     while (*(command + j) != ' ' && *(command + j) != '\0') {
  3840.         if (*(command + j) == '"')    /* Get quoted string           */
  3841.         for (*(strg0 + (i++)) = *(command + (j++));
  3842.              *(command + j) != '"';
  3843.              *(strg0 + (i++)) = *(command + (j++)));
  3844.         *(strg0 + (i++)) = *(command + (j++));
  3845.     }
  3846.     *(strg0 + (i++)) = '\0';/* NUL terminate every token   */
  3847.     }
  3848.     parms[k] = '\0';
  3849.  
  3850.     for (k = strlen(strg0) - 1; k >= 0; --k)    /* Convert to lower case       */
  3851.     *(strg0 + k) >= 'A' && *(strg0 + k) <= 'Z' ? *(strg0 + k) |= 32 : *(strg0 + k);
  3852. }
  3853.  
  3854. #endif                /* AMIGA_AC_5 */
  3855.  
  3856. #ifdef READLINE
  3857. char           *
  3858. rlgets(s, n, prompt)
  3859.     char           *s;
  3860.     int             n;
  3861.     char           *prompt;
  3862. {
  3863.     char           *readline();
  3864.     static char    *line = (char *) NULL;
  3865.  
  3866.     /* If we already have a line, first free it */
  3867.     if (line != (char *) NULL)
  3868.     free(line);
  3869.  
  3870.     line = readline((interactive) ? prompt : "");
  3871.  
  3872.     /* If it's not an EOF */
  3873.     if (line) {
  3874.     if (*line)
  3875.         add_history(line);
  3876.     strncpy(s, line, n);
  3877.     return s;
  3878.     }
  3879.     return line;
  3880. }
  3881. #endif                /* READLINE */
  3882.  
  3883. #if defined(MSDOS) || defined(_Windows) || defined(DOS386)
  3884.  
  3885. #ifndef _Windows
  3886. #ifdef __TURBOC__ 
  3887. /* cgets implemented using dos functions */
  3888. /* Maurice Castro 22/5/91 */
  3889. char           *
  3890. doscgets(s)
  3891.     char           *s;
  3892. {
  3893.     long            datseg;
  3894.  
  3895.     /* protect and preserve segments - call dos to do the dirty work */
  3896.     datseg = _DS;
  3897.  
  3898.     _DX = FP_OFF(s);
  3899.     _DS = FP_SEG(s);
  3900.     _AH = 0x0A;
  3901.     geninterrupt(33);
  3902.     _DS = datseg;
  3903.  
  3904.     /* check for a carriage return and then clobber it with a null */
  3905.     if (s[s[1] + 2] == '\r')
  3906.     s[s[1] + 2] = 0;
  3907.  
  3908.     /* return the input string */
  3909.     return (&(s[2]));
  3910. }
  3911. #endif                /* __TURBOC__ */
  3912. #endif                /* !_Windows */
  3913.  
  3914. #ifdef __ZTC__
  3915. void cputs(char *s)
  3916. {
  3917.    register int i = 0;
  3918.    while (s[i] != '\0')  bdos(0x02, s[i++], NULL);
  3919. }
  3920. char *cgets(char *s)
  3921. {
  3922.    bdosx(0x0A, s, NULL);
  3923.  
  3924.    if (s[s[1]+2] == '\r')
  3925.       s[s[1]+2] = 0;
  3926.  
  3927.    /* return the input string */
  3928.    return(&(s[2]));
  3929. }
  3930. #endif   /* __ZTC__ */
  3931.  
  3932.  
  3933. read_line(prompt)
  3934.     char           *prompt;
  3935. {
  3936.     register int    i;
  3937.     int             start = 0, ilen = 0;
  3938.     TBOOLEAN         more;
  3939.     int             last;
  3940.     char           *p, *crnt_prompt = prompt;
  3941.  
  3942.     if (interactive) {        /* if interactive use console IO so CED will
  3943.                  * work */
  3944. #ifndef READLINE
  3945. #if defined(_Windows) || defined(__EMX__) || defined(DJGPP) || defined(__ZTC__)
  3946.     printf("%s", prompt);
  3947. #else
  3948.     cputs(prompt);
  3949. #endif
  3950. #endif                /* READLINE */
  3951.     do {
  3952.         ilen = MAX_LINE_LEN - start - 1;
  3953.         input_line[start] = ilen > 126 ? 126 : ilen;
  3954. #ifdef READLINE
  3955.         input_line[start + 2] = 0;
  3956.         (void) rlgets(&(input_line[start + 2]), ilen, crnt_prompt);
  3957. #ifdef sequent
  3958.         if ((p = index(&(input_line[start + 2]), '\r'))!=NULL)
  3959.         *p = 0;
  3960.         if ((p = index(&(input_line[start + 2]), '\n'))!=NULL)
  3961.         *p = 0;
  3962. #else
  3963.         if ((p = strchr(&(input_line[start + 2]), '\r'))!=NULL)
  3964.         *p = 0;
  3965.         if ((p = strchr(&(input_line[start + 2]), '\n'))!=NULL)
  3966.         *p = 0;
  3967. #endif
  3968.         input_line[start + 1] = strlen(&(input_line[start + 2]));
  3969. #else                /* READLINE */
  3970. #if defined(_Windows) || defined(__EMX__) || defined(DJGPP)
  3971.         (void) gets(&(input_line[start+2]));
  3972. #else
  3973. #ifdef __TURBOC__
  3974.         (void) doscgets(&(input_line[start]));
  3975. #else                /* __TURBOC__ */
  3976.         (void) cgets(&(input_line[start]));
  3977. #endif                /* __TURBOC__ */
  3978. #endif                /* _Windows || __EMX__ || DJGPP*/
  3979.         (void) putc('\n', stderr);
  3980. #endif                /* READLINE */
  3981.         if (input_line[start + 2] == 26) {
  3982.         /* end-of-file */
  3983.         (void) putc('\n', stderr);
  3984.         input_line[start] = '\0';
  3985.         inline_num++;
  3986.         if (start > 0)    /* don't quit yet - process what we have */
  3987.             more = FALSE;
  3988.         else {
  3989.             (void) putc('\n', stderr);
  3990.             return(1); /* exit gnuplot */
  3991.             /* NOTREACHED */
  3992.         }
  3993.         } else {
  3994.         /* normal line input */
  3995.         register        i = start;
  3996.         while ((input_line[i] = input_line[i + 2]) != (char) NULL)
  3997.             i++;    /* yuck!  move everything down two characters */
  3998.  
  3999.         inline_num++;
  4000.         last = strlen(input_line) - 1;
  4001.         if (last<0) last=0;  /* stop UAE in Windows */
  4002.         if (last + 1 >= MAX_LINE_LEN)
  4003.             int_error("Input line too long", NO_CARET);
  4004.  
  4005.         if (input_line[last] == '\\') {    /* line continuation */
  4006.             start = last;
  4007.             more = TRUE;
  4008.         } else
  4009.             more = FALSE;
  4010.         }
  4011. #ifndef READLINE
  4012.         if (more)
  4013. #if defined(_Windows) || defined(__EMX__) || defined(DJGPP) || defined(__ZTC__)
  4014.         printf("> ");
  4015. #else
  4016.         cputs("> ");
  4017. #endif
  4018. #else
  4019.         crnt_prompt = "> ";
  4020. #endif                /* READLINE */
  4021.     } while (more);
  4022.     } else {            /* not interactive */
  4023.     if (interactive)
  4024.         fputs(prompt, stderr);
  4025.     do {
  4026.         /* grab some input */
  4027.         if (fgets(&(input_line[start]), MAX_LINE_LEN - start, stdin)
  4028.         == (char *) NULL) {
  4029.         /* end-of-file */
  4030.         if (interactive)
  4031.             (void) putc('\n', stderr);
  4032.         input_line[start] = '\0';
  4033.         inline_num++;
  4034.         if (start > 0)    /* don't quit yet - process what we have */
  4035.             more = FALSE;
  4036.         else
  4037.             return(1);  /* exit gnuplot */
  4038.         } else {
  4039.         /* normal line input */
  4040.         last = strlen(input_line) - 1;
  4041.         if (input_line[last] == '\n') {    /* remove any newline */
  4042.             input_line[last] = '\0';
  4043.             /* Watch out that we don't backup beyond 0 (1-1-1) */
  4044.             if (last > 0)
  4045.             --last;
  4046.             inline_num++;
  4047.         } else if (last + 1 >= MAX_LINE_LEN)
  4048.             int_error("Input line too long", NO_CARET);
  4049.  
  4050.         if (input_line[last] == '\\') {    /* line continuation */
  4051.             start = last;
  4052.             more = TRUE;
  4053.         } else
  4054.             more = FALSE;
  4055.         }
  4056.         if (more && interactive)
  4057.         fputs("> ", stderr);
  4058.     } while (more);
  4059.     }
  4060.     return(0);
  4061. }
  4062.  
  4063.  
  4064. do_shell()
  4065. {
  4066.     register char  *comspec;
  4067.     if ((comspec = getenv("COMSPEC")) == (char *) NULL)
  4068.     comspec = "\\command.com";
  4069. #ifdef _Windows
  4070.     if (WinExec(comspec, SW_SHOWNORMAL) <= 32)
  4071. #else
  4072. #ifdef DJGPP
  4073.     if (system(comspec) == -1)
  4074. #else
  4075.     if (spawnl(P_WAIT, comspec, NULL) == -1)
  4076. #endif
  4077. #endif
  4078.     os_error("unable to spawn shell", NO_CARET);
  4079. }
  4080.  
  4081. #else                /* MSDOS */
  4082. /* plain old Unix */
  4083.  
  4084. read_line(prompt)
  4085.     char           *prompt;
  4086. {
  4087.     int             start = 0;
  4088.     TBOOLEAN         more = FALSE;
  4089.     int             last = 0;
  4090.  
  4091. #ifndef READLINE
  4092.     if (interactive)
  4093.     fputs(prompt, stderr);
  4094. #endif                /* READLINE */
  4095.     do {
  4096.     /* grab some input */
  4097. #ifdef READLINE
  4098.     if (((interactive)
  4099.          ? rlgets(&(input_line[start]), MAX_LINE_LEN - start,
  4100.               ((more) ? "> " : prompt))
  4101.          : fgets(&(input_line[start]), MAX_LINE_LEN - start, stdin))
  4102.         == (char *) NULL) {
  4103. #else
  4104.     if (fgets(&(input_line[start]), MAX_LINE_LEN - start, stdin)
  4105.         == (char *) NULL) {
  4106. #endif                /* READLINE */
  4107.         /* end-of-file */
  4108.         if (interactive)
  4109.         (void) putc('\n', stderr);
  4110.         input_line[start] = '\0';
  4111.         inline_num++;
  4112.         if (start > 0)    /* don't quit yet - process what we have */
  4113.         more = FALSE;
  4114.         else
  4115.         return(1); /* exit gnuplot */
  4116.     } else {
  4117.         /* normal line input */
  4118.         last = strlen(input_line) - 1;
  4119.         if (input_line[last] == '\n') {    /* remove any newline */
  4120.         input_line[last] = '\0';
  4121.         /* Watch out that we don't backup beyond 0 (1-1-1) */
  4122.         if (last > 0)
  4123.             --last;
  4124.         } else if (last + 1 >= MAX_LINE_LEN)
  4125.         int_error("Input line too long", NO_CARET);
  4126.  
  4127.         if (input_line[last] == '\\') {    /* line continuation */
  4128.         start = last;
  4129.         more = TRUE;
  4130.         } else
  4131.         more = FALSE;
  4132.     }
  4133. #ifndef READLINE
  4134.     if (more && interactive)
  4135.         fputs("> ", stderr);
  4136. #endif
  4137.     } while (more);
  4138.     return(0);
  4139. }
  4140.  
  4141. #ifdef VFORK
  4142.  
  4143. do_shell()
  4144. {
  4145.     register char  *shell;
  4146.     register int    p;
  4147.     static int      execstat;
  4148.     if (!(shell = getenv("SHELL")))
  4149.     shell = SHELL;
  4150. #ifdef AMIGA_AC_5
  4151.     execstat = fexecl(shell, shell, NULL);
  4152. #else
  4153.     if ((p = vfork()) == 0) {
  4154.     execstat = execl(shell, shell, NULL);
  4155.     _exit(1);
  4156.     } else if (p == -1)
  4157.     os_error("vfork failed", c_token);
  4158.     else
  4159.     while (wait(NULL) != p)
  4160. #endif
  4161.         ;
  4162.     if (execstat == -1)
  4163.     os_error("shell exec failed", c_token);
  4164.     (void) putc('\n', stderr);
  4165. }
  4166. #else                /* VFORK */
  4167.  
  4168. #ifdef AMIGA_SC_6_1
  4169. do_shell()
  4170. {
  4171.     register char  *shell;
  4172.     if (!(shell = getenv("SHELL")))
  4173.     shell = SHELL;
  4174.  
  4175.     if (system(shell))
  4176.     os_error("system() failed", NO_CARET);
  4177.  
  4178.     (void) putc('\n', stderr);
  4179. }
  4180. #else                /* AMIGA_SC_6_1 */
  4181. #ifdef OS2
  4182. do_shell()
  4183. {
  4184.     register char  *shell;
  4185.     if (!(shell = getenv("COMSPEC")))
  4186.     shell = SHELL;
  4187.  
  4188.     if (system(shell) == -1 )
  4189.     os_error("system() failed", NO_CARET);
  4190.  
  4191.     (void) putc('\n', stderr);
  4192. }
  4193. #else                           /* ! OS2 */
  4194. #define EXEC "exec "
  4195. do_shell()
  4196. {
  4197.     static char     exec[100] = EXEC;
  4198.     register char  *shell;
  4199.     if (!(shell = getenv("SHELL")))
  4200.     shell = SHELL;
  4201.  
  4202.     if (system(strncpy(&exec[sizeof(EXEC) - 1], shell,
  4203.                sizeof(exec) - sizeof(EXEC) - 1)))
  4204.     os_error("system() failed", NO_CARET);
  4205.  
  4206.     (void) putc('\n', stderr);
  4207. }
  4208. #endif                          /* OS2 */
  4209. #endif                /* AMIGA_SC_6_1 */
  4210. #endif                /* VFORK */
  4211. #endif                /* MSDOS */
  4212. #endif                /* vms */
  4213.  
  4214. #ifdef _Windows
  4215. /* there is a system like call on MS Windows but it is a bit difficult to 
  4216.    use, so we will invoke the command interpreter and use it to execute the 
  4217.    commands */
  4218. int winsystem(s)
  4219. char *s;
  4220. {
  4221.     LPSTR comspec;
  4222.     LPSTR execstr;
  4223.     LPSTR p;
  4224.  
  4225.     /* get COMSPEC environment variable */
  4226. #ifdef WIN32
  4227.     char envbuf[81];
  4228.     GetEnvironmentVariable("COMSPEC", envbuf, 80);
  4229.     if (*envbuf == '\0');
  4230.         comspec = "\\command.com";
  4231.     else
  4232.         comspec = envbuf;
  4233. #else
  4234.     p = GetDOSEnvironment();
  4235.     comspec = "\\command.com";
  4236.     while (*p) {
  4237.         if (!strncmp(p,"COMSPEC=",8)) {
  4238.             comspec=p+8;
  4239.             break;
  4240.         }
  4241.         p+=strlen(p)+1;
  4242.     }
  4243. #endif
  4244.     /* if the command is blank we must use command.com */
  4245.     p = s;
  4246.     while ((*p == ' ') || (*p == '\n') || (*p == '\r'))
  4247.         p++;
  4248.     if (*p == '\0')
  4249.     {
  4250.         WinExec(comspec, SW_SHOWNORMAL);
  4251.         }
  4252.     else
  4253.     {
  4254.         /* attempt to run the windows/dos program via windows */
  4255.         if (WinExec(s, SW_SHOWNORMAL) <= 32)
  4256.         {
  4257.             /* attempt to run it as a dos program from command line */
  4258.             execstr = (char *) malloc(strlen(s) + strlen(comspec) + 6);
  4259.             strcpy(execstr, comspec);
  4260.             strcat(execstr, " /c ");
  4261.             strcat(execstr, s);
  4262.             WinExec(execstr, SW_SHOWNORMAL);
  4263.             free(execstr);
  4264.             }
  4265.         }
  4266.  
  4267.     /* regardless of the reality return OK - the consequences of */
  4268.     /* failure include shutting down Windows */
  4269.     return(0);        /* success */
  4270.     }
  4271. #endif
  4272.